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

replace in java with a regex doesn't replace from left to right

I'm trying to do a markdown parser. Basically, right now I just want to transform something like: "this is a *italic* text" into "this is a italic text". I have basically this: html_text = html_text.replaceAll("\*(.+)\*",…
Carlos
  • 105
  • 2
  • 14
-2
votes
2 answers

String.replace in Java

How can I transform the string st = "[123, 123, 134, 90]" into s = "123 123 134 90"
Shah
  • 661
  • 2
  • 9
  • 19
-3
votes
3 answers

Why 10 is in the beginning of an ArrayList after Sort() ? How to place 10 after 9?

String s = "vttqexwqgdc"; char[] ch = s.toCharArray(); int[] indices = {9, 5, 8, 0, 4, 3, 6, 10, 1, 2, 7}; ArrayList list = new ArrayList<>(); for (int i = 0, j = 0; i < ch.length; i++, j++) { list.add((indices[j] + "" +…
-3
votes
3 answers

How to leave HTML tags as it is and change text in java string

Want to leave HTML as it is in the Java String and to modify text String input = "
0i

2

b

h

5

"; .replace("0", "۰").replace("1", "&#۱;").replace("2", "&#۲;") .replace("3",…
Murteza
  • 57
  • 9
-3
votes
1 answer

regex excluding time from text

I would like to extract time from the text below using regex. Text: "Media: a few minutes ago, 3:25 pm uts" Regex pattern to select only time (ex: 3:25) from the above text?
meado
  • 17
  • 3
-3
votes
1 answer

What is use of .replaceAll("\\s+$","");

Please anyone tell me what is the difference between: .replaceAll("\\s",""); .replaceAll("\\s+",""); and .replaceAll("\\s+$","");
-3
votes
1 answer

Strange behaviour for String.replaceAll() with number string

String pre = "895"; System.out.println(pre); pre = pre.replaceAll(".", ""); System.out.println(pre); The output strangely is: 895 So all numbers are erased from the string. Why is this?
Thieri
  • 203
  • 1
  • 10
-3
votes
1 answer

Replacing character to numbers with regular expression

I want to replace a character in String to numbers. For instance, Input: abcdefghia Output: 1234567891 Actually in instead of numbers could be any other character even in other languages So my idea is to create a program that replaces characters…
SkyLexxX
  • 25
  • 7
-3
votes
1 answer

Java Replace All?

I´m trying to replace some characters in my string but it isnt working. Can you please help me? My code is: String test = "ABC?!"; test = test.replaceAll("\\?",""); test= test.replaceAll("\\!, ""); Thank you so much for helping me.
Sarah Xoxo
  • 115
  • 6
-3
votes
2 answers

a simple Guess letters game in java

i have just started writing a (guess letters game) as an assignment .. in java (the first player inputs 10 words and then 3 random characters will be hidden after that a second player will guess each character and if he guessed correctly then a…
Losha
  • 11
  • 2
-3
votes
1 answer

How do I replace everything except for the parameter String (Word)

public String plusOut(String str, String word){ String bob = str.replaceAll(^word,"+"); return bob; } Sample Input and Output: I want to replace everything in String(str) that's not String(word) with a + plusOut("1234xy5678", "xy") ==…
-3
votes
2 answers

Error while using the replaceAll() method

I have some code that looks like this: str.replaceAll('$', ' '); return str; What I'm trying to do here is replace every instance of $ in my string str with a space. But I get this error when I compile: incompatible types: char cannot be converted…
Beatrice
  • 3
  • 1
  • 3
-3
votes
1 answer

Need to escape "||" condition from a string

I have situation where i need to remove "||" and insert a backslash from a string. String s = "stack||overflow"; I need to replace and the resultant string should be "stack\overflow". I tried using replaceAll function but didn't work. Any help is…
Srikanth Sridhar
  • 2,317
  • 7
  • 30
  • 50
-3
votes
2 answers

Java Regex to replace text between braces {...} including newline

How do I replace text between braces {...} including newline with regex in java? I've seen many related questions about removing text within brackets but the main problem here is that if there's a new line in the middle of the match, it sort of…
bmo
  • 147
  • 1
  • 11
-3
votes
2 answers

java replace e replaceAll. ReplaceAll not working

I have this exercise. Why not work with replaceAll? I have an error: String index out of range: 1 public class e3 { public static void main(String[] args) { String x="Sessione successiva"; String nuova=x.replace("i", "!"); …
1 2 3
51
52