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
-1
votes
2 answers

Regular expression for splitting JSON text in lines after symbols

I am trying to use a regular expression to have this kind of string { "key1" : value1 , "key2" : "value2" , "arrayKey" : [ { "keyA" : valueA , "keyB" : "valueB" , "keyC" : [ 0 , 1 , 2 ] } ] } from JSONObject.toString() that is one long line of…
P5music
  • 3,197
  • 2
  • 32
  • 81
-1
votes
1 answer

How to introduce line break in a string whenever keyword occurs in java?

Objective: prefix each keyword with new line in a string code snippet: String [] keys = {"Status:","Active:","Priority:"}; text="Status: Open Active: Yes Priority: None"; System.out.println("Before: "+text); for(int k = 0; k
AVA
  • 2,474
  • 2
  • 26
  • 41
-1
votes
2 answers

How I can fix it this "replaceAll" error?

How I can fix it? String replace1 = WEBSITE.replaceAll("{fromNumber}", number); this character "{" error in replaceAll function. Thank you
Tanat29
  • 15
  • 1
-1
votes
3 answers

How to fix ReplaceAll function in java code

i am trying to replace all occurrences of the first character in a string with another using the replace all function. However, no change occurs when i run the function. I tried to target the first character of the original string and then carry the…
Chev
  • 1
  • 2
-1
votes
1 answer

(Java) How to mimic a regex in the "replacement" argument of String.replaceAll()?

Don't worry, I'm not asking you to help me find a regex! I'm currently using the method String.replaceAll(String regex, String replacement) to parse a line of user-provided data and grab the username. I'm doing this by stripping the line of…
-1
votes
1 answer

ASCII Char remove not functioning

If a string contains any non-ASCII value, i need to to delete it. I have tried replaceAll mrthod which is working fine with jdk 1.8 and above. but i want to deploy the same on jdk 1.6 and is it not functioning. String Remarks2 ="hii:╘’i"; String…
Shreyas
  • 11
  • 3
-1
votes
2 answers

Java replaceAll not working

Map contents: key (#500 - #509); and there values for (Map.Entry entry : values.entrySet()) { if (line.contains(entry.getKey())) { line = line.replaceAll(entry.getKey(), String.valueOf(entry.getValue())); …
Cocos
  • 27
  • 3
-1
votes
2 answers

ReplaceAll String java regex

I have a string that contains the following: "Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)" I want to replaceAll so I get rid of the brackets containing percentages but not the other numbers like so: "Hello my name is and bye also…
Kyril Khaltesky
  • 85
  • 1
  • 1
  • 7
-1
votes
1 answer

(Java) To replace with a slash, why are 4 slashes required in replacement argument of String's replaceAll method?

In Java, the string "\\" represents a single backlash, the first backslash being an escape character. Thus System.out.print("\\") prints \. However if "\\" is given as the replacement argument in method replaceAll, as in "aba".replaceAll("b", "\\"),…
wedge
  • 1
  • 2
-1
votes
1 answer

Java Regex ReplaceAll on range -> String.replaceAll() on string X after (not including) some char Y ~> until (including) end of case-ignored string Z

I am trying to replace all instances of a substring from the first character following ":" until the end of a substring "user" with case-ignored using the (?i) So, an example would be: Input u:sdneUSER|r:endsUser(&g:againuser) Replace All…
nelloreg
  • 11
  • 1
-1
votes
1 answer

Doing String.replaceAll multiple times ignoring previous letters replaced Java

I have a problem that now has come down to having two arraylists with the letters of the alphabet in them in some order. I want to replace all the letters in a string with the ones in the first arrraylist with the one in the second (i.e the element…
Tayyab Hussain
  • 107
  • 2
  • 10
-1
votes
1 answer

Replace special characters with dynamic content

Heys guys! I'm having difficulties replacing string which consists of special characters. So I have a following string for example: Dear *|customer_name|*, thank you for your order *|order_id|*. Please expect delivery *|delivery_date|* What I…
Igor
  • 253
  • 3
  • 5
  • 14
-1
votes
1 answer

How to reverse string length back to default length in Java?

I have two source files: searchworker and searchvalidator. The worker provide how program will work (logically) and validator is just validate user input. In my case, the system will run correctly if user input 9 digits of key (e.g. GGHYK1221) with…
-1
votes
2 answers

Using removeall to remove spaces between brackets() and numbers

I am trying to achieve something like this Input -> ( 10 ) I want output as (10) I want to eliminate space between brackets and numbers in Java using String.removeAll(), [Note: Only one space is there] Unable to write regular expression for…
user8397378
-1
votes
5 answers

How do get separate digits from a string in Java?

I 'am working on a file editor project and need to determine when a user inputs a number. If the user inputs D, it should delete the last line if they enter D 2 it will delete index 2 if they enter D 2 5 then the program should delete lines 2 -5…
Mrs.Brightside
  • 115
  • 1
  • 1
  • 12