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
136
votes
5 answers

String.replaceAll single backslashes with double backslashes

I'm trying to convert the String \something\ into the String \\something\\ using replaceAll, but I keep getting all kinds of errors. I thought this was the solution: theString.replaceAll("\\", "\\\\"); But this gives the below exception:…
Frank Groeneveld
  • 1,794
  • 3
  • 16
  • 23
74
votes
7 answers

replace \n and \r\n with
in java

This has been asked several times for several languages but I can't get it to work. I have a string like this String str = "This is a string.\nThis is a long string."; And I'm trying to replace the \n with
using str =…
Bala R
  • 107,317
  • 23
  • 199
  • 210
64
votes
2 answers

Removing all fraction symbols like “¼” and “½” from a string

I need to modify strings similar to "¼ cups of sugar" to "cups of sugar", meaning replacing all fraction symbols with "". I have referred to this post and managed to remove ¼ using this line: itemName = itemName.replaceAll("\u00BC", ""); but how do…
Michelle
  • 601
  • 5
  • 9
62
votes
7 answers

String replaceAll() vs. Matcher replaceAll() (Performance differences)

Are there known difference(s) between String.replaceAll() and Matcher.replaceAll() (On a Matcher Object created from a Regex.Pattern) in terms of performance? Also, what are the high-level API 'ish differences between the both? (Immutability,…
SuPra
  • 8,488
  • 4
  • 37
  • 30
55
votes
3 answers

How to change spaces to underscore and make string case insensitive?

I have following question. In my app there is a listview. I get itemname from listview and transfer it to the webview as a string. How to ignore case of this string and change spaces to underscores? For example: String itemname = "First Topic". I…
Sabre
  • 4,131
  • 5
  • 36
  • 57
53
votes
3 answers

Java regex replaceAll multiline

I have a problem with the replaceAll for a multiline string: String regex = "\\s*/\\*.*\\*/"; String testWorks = " /** this should be replaced **/ just text"; String testIllegal = " /** this should be replaced \n **/ just…
Robert
  • 565
  • 1
  • 5
  • 6
44
votes
8 answers

String replace a Backslash

How can I do a string replace of a back slash. Input Source String: sSource = "http://www.example.com\/value"; In the above String I want to replace "\/" with a "/"; Expected ouput after replace: sSource = "http://www.example.com/value"; I get the…
kensen john
  • 5,439
  • 5
  • 28
  • 36
35
votes
1 answer

JavaScript String replace vs replaceAll

ECMAScript 2021 has added a new String function replaceAll. A long time ago in a galaxy not so far away, people used split + join or regular expressions to replace all occurences of a string. I created the following example to compare the new method…
Baptistou
  • 1,749
  • 1
  • 13
  • 24
31
votes
5 answers

groovy: how to replaceAll ')' with ' '

I tried this: def str1="good stuff 1)" def str2 = str1.replaceAll('\)',' ') but i got the following error: Exception org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script11.groovy: 3: unexpected char: '\' @ line 3,…
john
  • 2,572
  • 11
  • 35
  • 51
27
votes
3 answers

Java String ReplaceAll method giving illegal repetition error?

I have a string and when I try to run the replaceAll method, I am getting this strange error: String str = "something { } , op"; str = str.replaceAll("o", "\n"); // it works fine str = str.replaceAll("{", "\n"); // does not work and i get a…
Johnydep
  • 6,027
  • 20
  • 57
  • 74
26
votes
3 answers

What does regex "\\p{Z}" mean?

I am working with some code in java that has an statement like String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","") I am not used to regex, so what is the meaning of it? (If you could provide a website to learn the basics of…
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
24
votes
6 answers

Java: Replace all ' in a string with \'

I need to escape all quotes (') in a string, so it becomes \' I've tried using replaceAll, but it doesn't do anything. For some reason I can't get the regex to work. I'm trying with String s = "You'll be totally awesome, I'm really terrible"; String…
Iwan Eising
  • 291
  • 1
  • 2
  • 7
19
votes
7 answers

String replaceAll("¾") in java

I have really weird error in my java code and can not figure out what is wrong. Let's say I have this code: private void test() { String test1 = replace("1.25"); String test2 = replace("1.5"); String test3 = replace("1.75"); } private…
Johan Nordli
  • 1,220
  • 3
  • 13
  • 26
18
votes
3 answers

Why is string.replaceAll() not a function on Android React Native?

I recently stumbled on this issue that, though ran well on iOS, could not run on Android, even though I use the same code base and run them in parallel. Note that (number + '') always returns a string, and this code runs on iOS. Trying the regex…
Hung Vu
  • 360
  • 3
  • 11
16
votes
5 answers

Why doesn't Java include the time/space complexity of each function in the javadoc?

Hi I want to know what is the time complexity of the "replaceAll" function of the String class but I can't find any information on it.(http://docs.oracle.com/javase/6/docs/api/java/lang/String.html) Wouldn't it be better for Java to include the…
jan1
  • 613
  • 2
  • 10
  • 17
1
2 3
51 52