Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 41
Registered: ‎11-18-2012
My Device: blackberry sms message making
Accepted Solution

How to replace Strings in Blackberry/j2me?

[ Edited ]

i want to replace a couple of strings from a text..i have seen that ,their is replace function which replace the characters but i want to replace the entire string...i think thier no such function in JDE .So i think i want make user defined function for replace the string...but am not getting the basic idea for how to do it 

 

So any one can help me in doing this...

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to replace Strings in Blackberry/j2me?

"replace a couple of strings from a text " doesn't give me a good idea of what you are trying to do..

 

Is the text in some type of BB Java control. like a text field maybe? When you say "strings", are you referring to the Java "String" class, or something else?

 

 

Contributor
Posts: 41
Registered: ‎11-18-2012
My Device: blackberry sms message making

Re: How to replace Strings in Blackberry/j2me?

i got the code and its working correctly...it will replace the one string with another...its good

 

My code is

 

public static String replace(String _text, String _searchStr, String _replacementStr)
 {
// String buffer to store str
StringBuffer sb = new StringBuffer();
 
// Search for search
int searchStringPos = _text.indexOf(_searchStr);
int startPos = 0;
int searchStringLength = _searchStr.length();
 
// Iterate to add string
while (searchStringPos != -1) {
sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
startPos = searchStringPos + searchStringLength;
searchStringPos = _text.indexOf(_searchStr, startPos);
}
 
// Create string
sb.append(_text.substring(startPos,_text.length()));
 
return sb.toString();
}