0

I have a String userId:344556\\ncustomerId:233 .

I want to replace the substring \\n with new line. So i tried

String more = strmoreInfo.replace("\\n", "\n");

My desired output is:

userId:344556
customerId:233

But my current output is:

userId:344556\
customerId:233

What am I doing wrong?

3 Answers 3

2

'\' is an escape character. You'll want to change the statement to:

String more = strmoreInfo.replace("\\\\n", "\n");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tah :) I missed that
0

I will try your code but i could not found any problem its works......

public class HelloWorld
{
  public static void main(String[] args)
  {    
    String strmoreInfo="userId:344556\\ncustomerId:233";
    String more = strmoreInfo.replace("\\n", "\n");
     System.out.println(more);
  }
}

Comments

0

Simply put a space e.g "< SPACE >\n" in second argument of replace method.

String more = strmoreInfo.replace("\\n", " \n");

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.