2

Suppose I have a string String text = ""; and I add a text to it

text = "Apple";

now I want it not to remove the old String when I add a new one, like this

text = "Banana";
text = "Orange"; 

The output should be Apple, Banana, Orange, but the output i'm getting is Orange. How to do that in java?

Sirmyself
  • 1,464
  • 1
  • 14
  • 29

3 Answers3

3

Since String is Immutable then you can't edit t without reassigning it, however there is something called StringBuilder it is mutable so it can be changed.

String original=new String("blabla")
StringBuilder builder=new StringBuilder(myString);

original will not be affected expect if did you re assign it, but because of String builder is mutable you can do something like the following

builder.appened("someData");

and it should be retrieved as string like this

String newString=builder.toString()
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
  • the out put i need `blabla, someData` but i can't see that! –  Mar 27 '18 at 17:23
  • the output should be blablasomedata – Basil Battikhi Mar 27 '18 at 17:24
  • `String original = new String("blabla"); StringBuilder builder = new StringBuilder(original); builder.append("someData");` –  Mar 27 '18 at 17:35
  • i'm getting `blablasomedata` –  Mar 27 '18 at 17:36
  • Yep exactly, but be aware that the original value was not changed so if you wrote System.out.println(orginal) It will print blabla – Basil Battikhi Mar 27 '18 at 17:38
  • actually that doesn't solve my problem, i'm using json (async task) and the string come from array `items[] : { "string: "bla1" },{ "string: "bla2" },{ "string: "bla3" }` now on result i want to catch it in a single string like `bla1bla2bla3` i thought i'll get this result but i won't –  Mar 27 '18 at 17:52
  • That wasnt what question asking for ! – Basil Battikhi Mar 27 '18 at 17:58
0

You want the variable to hold multiple Strings. Yet, you don't want to append the String. A data structure is what you are looking for.

In this case, ArrayList can fulfill your needs:

ArrayList<String> texts = new ArrayList<String>();
texts.add("Apple");
texts.add("Banana");
texts.add("Orange");

You can get the elemebt by the order you added the Strings with index beggining with 0:

Systen.out.println(texts.get(0));    // prints "Apple"

To print all the elements from the ArrayList:

System.out.println(texts);    // prints "Apple, Banana, Orange"
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • how to get all items together? `texts.get(all)` ?? –  Mar 27 '18 at 17:55
  • result i need is `AppleBananaOrange` –  Mar 27 '18 at 17:55
  • texts itself is already a collection of all items, hence you don't need (and can't) get all items. To print all elements, just print the entire list. As shown in my solution. – user3437460 Mar 27 '18 at 17:57
  • To aceess all elements from the list, use a loop. If my solution answers your question, pls accept my answer. – user3437460 Mar 27 '18 at 17:59
0

There are 2 ways you can achieve your desired output/result:

1. Using String:

You can simply append the new values at the end of String as following...

String text = "";
text = "Apple";
text = text + ", "+ "Banana";
text = text + ", "+ "Orange";

System.out.println(text); //output will be: Apple, Banana, Orange

2. Using StringBuilder: [recommended]

You can initialize StringBuilder, and use append() function to append new values at the end of the String and at last, you can get String from StringBuilder using toString() method of StringBuilder.

StringBuilder builder = new StringBuilder("");
builder.append("Apple").append(", ").append("Banana").append(", ").append("Orange");

String text = builder.toString();

System.output.println(text); //output will be Apple, Banana, Orange

2nd approach is recommended because when you append a String in StringBuilder, it does not create new object of String in String pool every time, it updates the existing object of StringBuilder.

Whereas String class in Java is immutable, so when you append a String in String literal, then it does update the existing object, it does create new object of String in String pool and points the reference to newly created String pool object.

Harsh Mehta
  • 571
  • 3
  • 16