BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-16-2009, 06:07 AM   #1 (permalink)
New Member
 
Join Date: Sep 2009
Model: 7100t
PIN: N/A
Carrier: o2
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default String replace

Please Login to Remove!

Hi,

I am trying to do something simple, however seem to be struggling on how to do this with the blackberry.

I have a string, I want to replace part of it with something else, however the only option seems to be replace char. Which for me is useless.

String str = "some words"

str.Replace("words","crisps");

However this is invalid as not a char.
Offline  
Old 10-16-2009, 07:00 AM   #2 (permalink)
New Member
 
Join Date: Oct 2009
Model: 8900
PIN: N/A
Carrier: Vodaphone
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

try this

Code:
String str = "some words";

        str = str.replace("words","crisps");
        
        System.out.println(str);
Offline  
Old 10-16-2009, 08:15 AM   #3 (permalink)
New Member
 
Join Date: Sep 2009
Model: 7100t
PIN: N/A
Carrier: o2
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the reply, however I get the following error

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
Offline  
Old 10-16-2009, 08:50 AM   #4 (permalink)
New Member
 
Join Date: Oct 2009
Model: 8900
PIN: N/A
Carrier: Vodaphone
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

well that a bit annoying sorry i was presuming that it would work as standard java.
That means your going to have to do some dodgy loop function to go through your string i.e...

Code:
 String str = "some words";

                int getIndex = str.indexOf("words");
                
                String tempFirstStr;
                String tempSecondStr;
                String tempStr = "";
                
                if(getIndex >= 0)
                {
                    if(getIndex >0)
                    {
                        tempStr = str.substring(0,getIndex);
                        tempStr += " crisps ";
                        tempStr += str.substring((getIndex + 5),str.length());
                    }
                    else
                    {
                        tempStr =  " crisps "+ str.substring((getIndex + 5),str.length());
                    }
                }
obviously this is simplistic as per your example youll probably have to come up with something a bit more complex.
Offline  
Old 10-16-2009, 09:31 AM   #5 (permalink)
New Member
 
Join Date: Sep 2009
Model: 7100t
PIN: N/A
Carrier: o2
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

cheers for that, will give it a go.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.