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
New Developer
Posts: 25
Registered: ‎03-20-2009
My Device: Not Specified

String replace

I finally have my appcompleted, except for one simple problem. I need to replace & with & in a string. I can't seem to find a way to do this.

 

Any help would be great.

 

Thanks.

 

-Matthew

Developer
Posts: 132
Registered: ‎02-11-2009
My Device: Not Specified

Re: String replace

JAVA 1.4 has a fancy String.replaceAll with regex.

We have to loop on indexOf('&'), and substring the parts together.

 

Cheers, Barak.

New Developer
Posts: 3
Registered: ‎04-25-2009
My Device: Not Specified

Re: String replace

This code could help :

 

public static String replaceAll(String source, String pattern,
            String replacement) {
        if (source == null) {
            return "";
        }
       
        StringBuffer sb = new StringBuffer();
        int idx = -1;
        int patIdx = 0;

        while ((idx = source.indexOf(pattern, patIdx)) != -1) {
            sb.append(source.substring(patIdx, idx));
            sb.append(replacement);
            patIdx = idx + pattern.length();
        }
        sb.append(source.substring(patIdx));
        return sb.toString();

    }

 

ComputerInBed

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

Re: String replace

How did you get the & in the text in the first place?

 

The only time I hit this issue was parsing HTML - I had to write an entity resolver class to resolve *all* of the HTML entities, inluding & 

 

 

 

Developer
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: String replace

regexp is available for j2me here. I haven't tried it with blackberry but it shouldn't take too much tweaking. You may have to define a symbol

in the JDE if you want unicode.....since it was written for Netbeans.

 

http://code.google.com/p/regexp-me/

Good Luck.

-Donald

New Developer
Posts: 3
Registered: ‎04-25-2009
My Device: Not Specified

Re: String replace

Regexp-me is under Apache 2 license,

 

anyone knows if it's possible to use it in a commercial product ?

 

Regards

Highlighted
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: String replace

regarding apache2 license
http://tinyurl.com/d2gkl7
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter