0

I have many string and i put it in a list.All the strings have this form:

\n Ajax\n

\n JavaScript\n

I just retreived from the server and i put in a list:

takedata.add(result.getBody().getTitle());

How can i cut the \n at both sides?

5
  • Is that a string of length two containing a slash and an n, or is it a newline character? Commented May 18, 2012 at 7:33
  • slash and n.I retrieve from a server and i put it in a list.And it put it with this form.And i want to delete the \n in both sides Commented May 18, 2012 at 7:34
  • @AlexDowining: Can you show us what already you have done before? Commented May 18, 2012 at 7:35
  • str.replaceAll("\n",""); Commented May 18, 2012 at 7:35
  • Show us your code, how do you put the String in ArrayList.. Commented May 18, 2012 at 7:35

2 Answers 2

4
List<String> list = new ArrayList<String>();
list.add("\n Ajax\n");
list.add("\n JavaScript\n");

for(int i = 0; i < list.size(); i++)
{
    String s = list.get(i).trim();
    list.set(i, s);
}

EDIT:

I just retreived from the server with and i put in a list:

takedata.add(result.getBody().getTitle());

How can i cut the \n at both sides?

Simply, use trim():

takedata.add(result.getBody().getTitle().trim());
Sign up to request clarification or add additional context in comments.

Comments

0

The title says ArrayList, so

if you have ArrayList<String> list, you can use the method replaceAll(String what, String withWhat)

And the method should be called on list.get(someIndex)

IE> list.get(1).replaceAll("\\n","") Remember you need to insert \ \ in order for java to read that you want to replace the \

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.