0

I'm trying to replace multiple substrings in a string, for example I have the following string wordlist

one    two    three

Where I want to replace \t tab characters with \r\n new line characters.

I define the separator variable as \n and replacement variable as \r\n.

Then I use wordlist = wordlist.replaceAll(separator, replacement); to replace all the characters, but when I display the wordlist again, it gives me the following result

onerntwornthree

I also tried splitting the wordlist by the substring separator into an array and then joining it again word by word into a new string separated by the replacement, but then it just gave me a result as

one\r\ntwo\r\nthree

Does anybody know how to solve this problem? In case you need it, here's the whole code:

System.out.print("Separator to replace: ");
separator = scanner.next( );

System.out.print("Replacement for separator: ");
replacement = scanner.next( );

wordlist = wordlist.replaceAll(separator, replacement);
4
  • Show the code you used. Commented Jul 14, 2014 at 12:18
  • I explained the whole idea, but if it is relevant, then I edited the post. Commented Jul 14, 2014 at 12:20
  • wordlist.replaceAll("\t", "\r\n") seems to work just fine: ideone.com/sdk1Dt Commented Jul 14, 2014 at 12:21
  • @danrodi: you query explicitly for the separator. And then strings are interpreted unescaped... Commented Jul 14, 2014 at 12:29

3 Answers 3

3

Your input character for tab seems to be incorrect.

This code gives

    String wordlist="one    two three";
    wordlist = wordlist.replaceAll("\t", "\r\n");
    System.out.println(wordlist);

This output-

one
two
three
Sign up to request clarification or add additional context in comments.

1 Comment

This is not portable, though.
0

What you want to do is probably to split the string and the write the different lines one at a time to a PrintStream. That way you can use println.

Java is a platform independent language, and new lines are platform dependent. Making use of PrintStream.println will make sure your code is portable.

Comments

0

Why do you set the separator to \n?, it should be \t I assume?

The following code works fine for jdoodle:

String s = "one\ttwo\tthree";
s = s.replaceAll("\t","\r\n");
System.out.println(s);

EDIT

The reason why this doesn't work is because you query the user for the separator and when he enters \t, this is a string with the first character \ and the second t and not an escape character.

You should use StringEscapeUtils.unescapeJava first.

Thus:

Scanner sc = new Scanner(System.in);
String separator = sc.nextLine();
separator = StringEscapeUtils.unescapeJava(separator);
String s = "one\ttwo\tthree";
s = s.replaceAll(separator,"\r\n");
System.out.println(s);

If org.apache.commons.lang.StringEscapeUtils is not available, you can do this explicitly:

Scanner sc = new Scanner(System.in);
String separator = sc.nextLine();
separator = separator.replaceAll("\\t","\t");
String s = "one\ttwo\tthree";
s = s.replaceAll(separator,"\r\n");
System.out.println(s);

demo

5 Comments

I tried the second option without the StringEscapeUtils, but it still keeps giving me the same result. Note that the replacement is also asked from the user and the separator doesn't have to be \t necessarily, it could also be \n.
Well, how do you enter the replacement? With tabs or with (explicit) "\t"?
If using the example I explained my problem with, when asked for separator I type in \t and press Enter, then I'm asked for replacement where I type in \r\n and press Enter again.
You need to perform two StringEscapeUtils.unescapeJava (on both separator and replacement)...
Used separator = separator.replaceAll("\\t","\t"); and replacement = replacement.replaceAll("\\r\n","\r\n");, still the same.

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.