d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > String Replace Method
12Next
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Apr 19 2013 04:14pm
Code
public class ChangeString
{
public static void main(String[] args)
{
 String test = "This is a test";
 test.replaceAll(" ", "%20");
 System.out.println(test);
}
}


The print gives me, "This is a test"

I don't understand why it is not giving me, "This%20is%20a%20test"

Thanks for the help!

This post was edited by xandumx on Apr 19 2013 04:31pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Apr 19 2013 04:40pm
read the spec for replaceAll

here's another hint: Strings are immutable

This post was edited by irimi on Apr 19 2013 04:40pm
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Apr 19 2013 04:50pm
Quote (irimi @ Apr 19 2013 03:40pm)
read the spec for replaceAll

here's another hint: Strings are immutable

Spec:
Replaces each substring of this string that matches the given regular expression with the given replacement.

I am finding examples of people using this just like I am and saying it works. I am not sure what the replace or replace all function is used for if not for this...
Member
Posts: 3,451
Joined: Feb 26 2010
Gold: 0.20
Apr 19 2013 04:53pm
public String replaceAll(String regex, String replacement)

replaceAll returns a new string, it doesn't change the string passed in
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Apr 19 2013 04:54pm
you missed the most important part of the spec, which is the method signature
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 19 2013 04:54pm
replace all uses a regular expression.

try again using replace()

Quote
public String replaceAll(String regex, String replacement)

replaceAll returns a new string, it doesn't change the string passed in


this as well

This post was edited by AbDuCt on Apr 19 2013 04:55pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Apr 19 2013 04:55pm
Quote (AbDuCt @ Apr 19 2013 03:54pm)
replace all uses a regular expression.

try again using replace()


surprisingly that's not the issue. you *can* use a regex but it's not necessary (i think. actually, maybe i'm wrong about this).

This post was edited by irimi on Apr 19 2013 04:57pm
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Apr 19 2013 05:04pm
Code
public static void main(String[] args)
{
 String test = "This is a test";
 test = test.replace(" ", "%20");
 System.out.println(test);
}


Thanks guys!

This post was edited by xandumx on Apr 19 2013 05:04pm
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Apr 22 2013 06:20am
For the sake of completeness, you should use StringBuffer (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html) when doing operations such as replace, concatenation etc on strings.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 22 2013 11:27am
Quote (m0hawk @ Apr 22 2013 08:20am)
For the sake of completeness, you should use StringBuffer (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html) when doing operations such as replace, concatenationetc on strings.


1) stringbuilder > stringbuffer in most cases
2) not much point in using sb during concatentation unless loops or similar are involved. the compiler optimizes string literal concatenation. it'll also convert variable concatenation, but not it tends to suck in loops because it'll keep creating new sbs.
3) there isn't really a performance benefit to using replace just once. you still have the original string and the new string as two separate objects either way.

This post was edited by carteblanche on Apr 22 2013 11:28am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll