5 Replies - 375 Views - Last Post: 21 March 2013 - 04:20 AM Rate Topic: -----

#1 SparklyLlama  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-March 13

Change Contents of String with an If statement?

Posted 20 March 2013 - 01:13 AM

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!
Is This A Good Question/Topic? 0
  • +

Replies To: Change Contents of String with an If statement?

#2 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

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 :)

This post has been edited by oha055: 20 March 2013 - 02:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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

Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

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. :)
Was This Post Helpful? 1
  • +
  • -

#5 SparklyLlama  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 12-March 13

Re: Change Contents of String with an If statement?

Posted 20 March 2013 - 10:31 PM

View PostRyano121, on 20 March 2013 - 04:35 AM, said:

Or more simply

if(y.equals("something")) {
   x = "new string";
}

This is not working for me, it just returns the original string.
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

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:
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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1