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
7
votes
9 answers

Replace all "(" and ")" in a string in Java

How to replace all "(" and ")" in a string with a fullstop, in Java? I tried in the following way: String url = "https://bitbucket.org/neeraj_r/url-shortner)"; url.replaceAll(")", "."); url.replaceAll(")", "."); But it does not work. The error…
Neeraj
  • 1,612
  • 7
  • 29
  • 47
6
votes
2 answers

Add spaces using kotlin string functions/string format

This might been asked here couple of times.. what i am trying to do adding space between every four char of a string(8888319024981442). my string length is exactly 16. String.format is not helpful avoiding the usage of split or creating multiple…
6
votes
3 answers

PySpark replace value in several column at once

I want to replace a value in a dataframe column with another value and I've to do it for many column (lets say 30/100 columns) I've gone through this and this already. from pyspark.sql.functions import when, lit, col df = sc.parallelize([(1, "foo",…
Ali
  • 7,810
  • 12
  • 42
  • 65
6
votes
2 answers

Java: Understanding the String replaceAll() method

I'm looking to figure out the answer to this problem here. First off, blah[abc] = blah[abc].replaceAll("(.*) (.*)", "$2, $1"); Can someone explain to me what the (.*), $2 and $1 are? Secondly, when I nest that within a for statement in order to…
Jds
  • 63
  • 1
  • 3
6
votes
3 answers

Java replaceAll regex error

I want to transforme all "*" into ".*" excepte "\*" String regex01 = "\\*toto".replaceAll("[^\\\\]\\*", ".*"); assertTrue("*toto".matches(regex01));// True String regex02 = "toto*".replaceAll("[^\\\\]\\*",…
brouille
  • 295
  • 4
  • 14
6
votes
1 answer

Java replaceAll("\\s+") vs replaceAll("\\\\s+")

What's the difference between replaceAll("\\s+") and replaceAll("\\\\s+")? Usually I use \\s+ but sometimes I see \\\\s+.
Maxim Gotovchits
  • 729
  • 3
  • 11
  • 22
6
votes
2 answers

How to ensure replaceAll will replace a whole word and not a subString

I have an input of dictionary. The dictionary is iterated over to replace the key from dictionary in the text. But replaceAll function replaces the subString as well. How to ensure that it will match the whole word (as a whole and not as a…
user2832203
  • 85
  • 2
  • 9
6
votes
2 answers

Remove certain characters from string

I want to create a program that gives the number of characters, words, etc... in a user-inputted string. To get the word count I need to remove all the periods and commas form a string. So far I have this: import javax.swing.JOptionPane; public…
f3d0r
  • 541
  • 3
  • 12
  • 27
6
votes
3 answers

How to replaceAll special characters in a string?

So to remove all the spaces in my string. I did a method that is consists of message = message.replaceAll("\\s", ""); I was wondering if there was a command to remove and special character, like a comma, or period and just have it be a string. Do i…
user2743857
  • 81
  • 1
  • 1
  • 6
6
votes
2 answers

Convert java string to string compatible with a regex in replaceAll

Is there a library or any easy way to convert a string and make sure its compatible as a regex to look for and replace in another string. So if the string is "$money" it would get converted to "\$money". I tried using StringEscapeUtil.escape but it…
Kevin Colin
  • 347
  • 3
  • 4
  • 10
5
votes
2 answers

Replace multiple chars with multiple chars in string

I am looking for a possibility to replace multiple different characters with corresponding different characters in Kotlin. As an example I look for a similar function as this one in PHP: str_replace(["ā", "ē", "ī", "ō", "ū"], ["a","e","i","o","u"],…
5
votes
1 answer

How to replace Backslash '\' with double Backslash in Dart?

How to replace a Single Backslash '\' in a String with double Backslash '\' ? I tried this, but its not working. main(){ String string = "back\slash back\slash back\slash back\slash"; String replaced = string.replaceAll(RegExp(r'\\'),…
Shahzad Akram
  • 4,586
  • 6
  • 32
  • 65
5
votes
4 answers

Replace substring (replaceAll) workaround

I'm trying to replace a substring that contains the char "$". I'd be glad to hear why it didnt works that way, and how it would work. Thanks, user_unknown public class replaceall { public static void main(String args[]) { String s1=…
user_unknown
  • 195
  • 2
  • 4
  • 7
5
votes
3 answers

Replace all occurrences of group

I want to replace all the occurrences of a group in a string. String test = "###,##.##0.0########"; System.out.println(test); test = test.replaceAll("\\.0(#)", "0"); System.out.println(test); The result I am trying to obtain is…
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
5
votes
3 answers

How to remove embedded braces in Java string?

I have a Java string that may contain "{" and "}" pairs, multiple times, inside a larger string: "My name is {name}. I am a {animal}. I like to {activity} whenever it rains." etc. I am trying to write code that would strip out all instances of…
user1768830
1 2
3
51 52