2

For example

String temp = "welcome,to,the,world,of,j2ee";

converting this to arraylist

ArrayList al= new ArrayList(temp.split(","));

this comes as content of arraylist as {"welcome","to","the","world","j2ee"}

my requirement is adding "^d" at end of every string

for ex {"welcome^d","to^d","the^d","world^d","j2ee^d"}

1
  • 3
    what did you try already? Commented Feb 25, 2014 at 6:37

4 Answers 4

7

Try this:

List<String> al= Arrays.asList((temp.replaceAll(",", "^d,") + "^d").split(","));
Sign up to request clarification or add additional context in comments.

Comments

1

Before the split, you could add the ^d. For example:

String temp = "Welcome,to,the,world,of,j2ee";
ArrayList<String> list = Lists.newArrayList(temp.replaceAll(",", "^d,").split(","));

1 Comment

This will not add ^d to the last element of the list
0

String.split does not return an array list, it returns a regular array. If you use a regular array then just concat ^d onto each value.

String temp = "welcome,to,the,world,of,j2ee";
    String[] al= temp.split(",");
    for(int i = 0; i < al.length; i++){
        al[i] += "^d";
    }
    for(String w : al){
    System.out.println(w);
    }

Comments

0
    String temp = "welcome,to,the,world,of,j2ee";
    temp = (temp + "^d").replace(",", "^d,");
    List<String> al= Arrays.asList(temp.split(","));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.