I have a program where String X is created, but I want to add in an If statement where if String Y equals a certain word, it changes what String X is.
Is this possible? If so, how could I do it?
Thanks!
Change Contents of String with an If statement?
Page 1 of 15 Replies - 161 Views - Last Post: 21 March 2013 - 04:20 AM
Replies To: Change Contents of String with an If statement?
#2
Re: Change Contents of String with an If statement?
Posted 20 March 2013 - 02:33 AM
Sure it's possible 
consider a method createString which returns a String:
createString(input / string x) {
if(check if string x equals word) set string x to the other word
return x
}
Post your code if you want more specific help
consider a method createString which returns a String:
createString(input / string x) {
if(check if string x equals word) set string x to the other word
return x
}
Post your code if you want more specific help
This post has been edited by oha055: 20 March 2013 - 02:35 AM
#3
Re: Change Contents of String with an If statement?
Posted 20 March 2013 - 04:35 AM
Or more simply
if(y.equals("something")) {
x = "new string";
}
This post has been edited by Ryano121: 20 March 2013 - 04:35 AM
#4
Re: Change Contents of String with an If statement?
Posted 20 March 2013 - 04:47 AM
Technically the string doesn't change, it makes a completely new string and then makes x refer to the new string, leaving the old string abandoned. A small detail, but an important one I feel.
#5
Re: Change Contents of String with an If statement?
Posted 20 March 2013 - 10:31 PM
#6
Re: Change Contents of String with an If statement?
Posted 21 March 2013 - 04:20 AM
Strings are immutable. The only thing you can change is the reference variable's pointer.
For example:
Whiskey Tango Foxtrot!?
I just appended " I love you." to my "Hello World", but where did it go?
To put it bluntly, it got eaten by poor forethought.
When you made your String s, it was created as "Hello World". That can't change. So if you try to change s, all that will happen is that a new String will be created, but it will be left without a reference variable and will float around until collected as garbage. Shame.
To get around that, to append "I love you" to my String, I'd have to do this.
All assignments have the same result. The important thing to remember is ALWAYS REASSIGN THE STRING WHEN YOU CHANGE IT.
Why is the String class immutable? Well, once you get your head around this, and after I've eaten my breakfast, I might tell you.
For example:
String s = "Hello World";
System.out.println(s); //The result is "Hello World" as expected.
s.concat(", I love you.");
System.out.println(s); //The result is "Hello World" ... ?
Whiskey Tango Foxtrot!?
I just appended " I love you." to my "Hello World", but where did it go?
To put it bluntly, it got eaten by poor forethought.
When you made your String s, it was created as "Hello World". That can't change. So if you try to change s, all that will happen is that a new String will be created, but it will be left without a reference variable and will float around until collected as garbage. Shame.
To get around that, to append "I love you" to my String, I'd have to do this.
s+=", I love you";
//OR
s = s + ", I love you";
//OR
s = s.concat(", I love you");
All assignments have the same result. The important thing to remember is ALWAYS REASSIGN THE STRING WHEN YOU CHANGE IT.
Why is the String class immutable? Well, once you get your head around this, and after I've eaten my breakfast, I might tell you.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|