Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have a string variable and another string assigned to same variable. Then how to get old string?

Tags:

java

string

Suppose I have a string

msg = "hello"

Now I get substring from string msg like msg.substring(1,msg.length())

And storing that substring into msg.

So msg refers to new string. If the old string also remain in buffer of java and yes then how to access it?

like image 248
uveng Avatar asked Dec 19 '22 14:12

uveng


1 Answers

Note that String.substring() implementation and the actual implementation of String has changed with Java 7 (release 6, IIRC)

So depending on your Java version:

  1. if it's post Java 7u6, you can't do this. String.substring() will give you a completely new string
  2. if it's an older version of Java, you could access the private underlying char array of your new String, and this is in fact the complete char array of your old string

Item 2 is particularly nasty, and I suspect beyond what you really want to achieve.

See here for more details behind the substring() implementation and how it's changed.

like image 58
Brian Agnew Avatar answered Jan 25 '23 23:01

Brian Agnew