Questions tagged [replaceall]

The replaceAll() method in Java is used to replace parts of a string using a regular expression.

The replaceAll() method in Java is used to replace parts of a string using a regular expression.

Note that in most languages, including Java, the String object is immutable. In other words, you can't change it. If you want a slightly different string value then you have to create a second object. For that reason, replaceAll() does not actually change the string being searched, but rather returns a new string that is the result of the specified search and replace actions.

780 questions
15
votes
4 answers

java replaceAll not working for \n characters

I have a string like this: John \n Barber now I want to replace \n with actual new line character so it will become John Barber this is my code for this replaceAll("\\n", "\n"); but it is not working and giving me same string John \n Barber
user2790289
  • 193
  • 1
  • 3
  • 6
13
votes
2 answers

How to preserve newlines while reading a file using stream - java 8

try (Stream lines = Files.lines(targetFile)) { List replacedContent = lines.map(line -> StringUtils.replaceEach(line,keys, values)) …
A.R.K.S
  • 1,692
  • 5
  • 18
  • 40
12
votes
2 answers

Escape space with backslash in java

I want to replace spaces from path string. I tried below but doesn't seems to be working : String path = "/Users/TD/San Diego"; path=path.replaceAll(" ","\\ "); System.out.println(path); Goal is to convert "/Users/TD/San Diego" to "/Users/TD/San\…
user2679590
10
votes
3 answers

Regex using Java String.replaceAll

I am looking to replace a java string value as follows. below code is not working. cleanInst.replaceAll("[]", ""); cleanInst.replaceAll("[]", ""); cleanInst.replaceAll("[//]", "/"); …
user2072797
  • 161
  • 1
  • 3
  • 6
9
votes
5 answers

Getting " when trying to replace a character in string

I want to replace " from a string with ^. String str = "hello \"there"; System.out.println(str); String str1 = str.replaceAll("\"", "^"); System.out.println(str1); String str2= str1.replaceAll("^", "\""); System.out.println(str2); and the output is…
Kumar Harsh
  • 423
  • 5
  • 26
9
votes
5 answers

Replace '\n' by ',' in java

I want to take input from user as String and replace the newline character \n with , I tried : String test ="s1\ns2\ns3\ns4"; System.out.println(test.replaceAll("\n",",")); Output was s1,s2,s3,s4 But when I try the same code by getting input…
Ramya Selvarani
  • 499
  • 1
  • 9
  • 23
9
votes
6 answers

How to replace the characher `\n` as a new line in android

I have one server response for an API request as shown below. Success! Your request has been sent.\n\nWe’ll inform you once it is done. This message I need to show in a Snackbar. I need new line to be added in the place of \n in this response . I…
user4260260
8
votes
3 answers

String replaceAll not replacing i++;

String preCode = "helloi++;world"; String newCode = preCode.replaceAll("i++;", ""); // Desired output :: newCode = "helloworld"; But this is not replacing i++ with blank.
Android Guy
  • 573
  • 1
  • 8
  • 18
7
votes
7 answers

Replace certain string in array of strings

let's say I have this string array in java String[] test = {"hahaha lol", "jeng jeng jeng", "stack overflow"}; but now I want to replace all the whitespaces in the strings inside the array above to %20, to make it like this String[] test =…
imin
  • 4,504
  • 13
  • 56
  • 103
7
votes
3 answers

java new line replacement

I am wondering about why I don't get the expected result with this one: String t = "1302248663033 "; t.replaceAll("\n", ""); System.out.println(t); The output is: 1302248663033 …
delmet
  • 1,013
  • 2
  • 9
  • 23
7
votes
4 answers

Regular expression to replace content between parentheses ()

I tried this code: string.replaceAll("\\(.*?)",""); But it returns null. What am I missing?
Praneel PIDIKITI
  • 18,677
  • 13
  • 41
  • 60
7
votes
4 answers

Matching a whole word with leading or trailing special symbols like dollar in a string

I can replace dollar signs by using Matcher.quoteReplacement. I can replace words by adding boundary characters: from = "\\b" + from + "\\b"; outString = line.replaceAll(from, to); But I can't seem to combine them to replace words with dollar…
john k
  • 6,268
  • 4
  • 55
  • 59
7
votes
3 answers

What is the Java equivalent to this preg_replace?

wordword word"; $str = preg_replace("/word(?!([^<]+)?>)/i","repl",$str); echo $str; # repl repl ?> source:…
celsowm
  • 846
  • 9
  • 34
  • 59
7
votes
2 answers

Java replaceAll with backreferences

Possible Duplicate: String.replaceAll() anomaly with greedy quantifiers in regex I was writing code that uses Matcher#replaceAll and found following result highly confusing: Pattern.compile("(.*)").matcher("sample").replaceAll("$1abc"); Now, I…
Wejn
  • 356
  • 3
  • 4
  • 13
7
votes
2 answers

replace() and replaceAll() in Java

The following code uses the replace() method of the String class in Java. String a = "abc/xyz"; System.out.println(a.replace("/", "\\")); / in the given String a is being replaced with \. The same thing is wrong, if we use the replaceAll() method…
Tiny
  • 27,221
  • 105
  • 339
  • 599
1
2
3
51 52