Change string in Java

I want to send a string to a method and change the string there. The method should return void. Example:

String s = "Hello"; ChangeString(s); String res = s; //res = "HelloWorld" ------------------------------------------- private void ChageString(String s){ s = s + "World"; } 

How can I do this in Java? Can this be done without adding another class?

Thanks! PB

+4
source share
6 answers

Your method cannot work with the current interface due to two reasons:

  • Lines are immutable. Once you have created a string, you cannot subsequently modify this string object.
  • Java uses pass-by-value, not pass-by-reference. When you assign a new value to s in your method, it only changes the local s , not the original s in the calling code.

For your method to work, you need to change the interface. The simplest change is to return a new line:

 private String changeString(String s){ return s + "World"; } 

Then name it like this:

 String s = "Hello"; String res = changeString(s); 

See how it works on the Internet: ideone

+8
source

No, this cannot be done by returning void .

Java passes parameters by value. Rows are immutable, so you cannot change your value. You were unlucky.

You can do it:

 private String changeString(String original) { return original + " World"; } 
+1
source

You can end your line in an object:

 public class StringWrapper { String _str; public StringWrapper(String str) { set(str); } public void set(String str) { _str = str; } public String toString() { return _str; } } 

Resolution:

 StringWrapper s = new StringWrapper("Hello"); ChangeString(s); String res = s + ""; //res = "HelloWorld" 
 private void ChageString(StringWrapper s) { s.set(s + "World"); } 
+1
source

No, the Java string is immutable and cannot be changed. You can simply return a new line and assign it to the same variable.

Also: String is declared final, so you cannot extend String to add new behavior

0
source

Use StringBuffer / StringBuilder to change the string. Strings in java are immutable, which means that after assigning a value, they cannot be changed, the code that seems to change the string actually creates a new object. Here is something in the line buffer, hope this helps. :)

0
source

Use StringBuffer instead of String, this will solve your problem.

 public static void main(String[] args) { StringBuffer s = new StringBuffer("Hello"); changeString(s); String res = s.toString(); //res = "HelloWorld" } private static void changeString(StringBuffer s){ s.append("World"); } 

Or, if you really need to use only String, so here is a solution using reflection:

 public static void main(String[] args) { String s = "Hello"; changeString(s); String res = s; //res = "HelloWorld" } private static void changeString(String s){ char[] result = (s+"World").toCharArray(); try { Field field = s.getClass().getDeclaredField("value"); field.setAccessible(true); field.set(s, result); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1) { e1.printStackTrace(); } } 
0
source

All Articles