26
votes
7answers
36k views

Java - replace a character at a specific index in a string?

I'm new to Java and I'm trying to replace a character at a specific index in a string. What I'm doing is: String myName = "domanokz"; myName.charAt(4) = 'x'; this gives me error. It seems like ...
9
votes
5answers
7k views

How to replace case-insensitive literal substrings in Java

Using the method replace(CharSequence target, CharSequence replacement) in String, how can I make the target case-insensitive? For example, the way it works right now: String target = "FooBar"; ...
9
votes
6answers
18k views

Replace last part of string

I want to replace the last String which is a , with ) Suppose the string is: Insert into dual (name,date, to be converted to: Insert into dual (name,date)
6
votes
5answers
4k views

Hints for java.lang.String.replace problem?

I would like to replace "." by "," in a String/double that I want to write to a file. Using the following Java code double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176 ...
13
votes
6answers
15k views

Java Replacing multiple different substring in a string at once (or in the most efficient way)

I need to replace many different sub-string in a string in the most efficient way. is there another way other then the brute force way of replacing each field using string.replace ?
28
votes
4answers
34k views

Java how to replace 2 or more spaces with single space in string and delete leading spaces only

Looking for quick, simple way in Java to change this string " hello there " to something that looks like this "hello there" where I replace all those multiple spaces with a single space, ...
24
votes
6answers
24k views

Strip Leading and Trailing Spaces From Java String [duplicate]

Possible Duplicate: trim whitespace from a string? Is there a convenience method to strip any leading or trailing spaces from a Java String? Something like: String myString = " keep this ...
7
votes
8answers
38k views

How do I replace a character in a string in Java?

Using Java, I want to go through the lines of a text and replace all ampersand symbols (&) with the XML entity reference &. I scan the lines of the text and then each word in the text ...
4
votes
3answers
5k views

Java : replacing text URL with clickable HTML link

I am trying to do some stuff with replacing String containing some URL to a browser compatible linked URL. My initial String looks like this : "hello, i'm some text with an url like ...
0
votes
4answers
1k views

Justify text in Java

I have to read in an integer which will be the length of the succeeding lines. (The lines of text will never be longer than the length provided). I then have to read in each line of text and convert ...
11
votes
5answers
30k views

Java string replace and the NUL (NULL, ASCII 0) character?

Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII characters. Taking a dip into the source I found this tidbit: // remove any periods from first name e.g. Mr. John ...
9
votes
2answers
3k views

Backreferences syntax in replacement strings (why dollar sign?)

In Java, and it seems in a few other languages, backreferences in the pattern is preceded by a slash (e.g. \1, \2, \3, etc), but in a replacement string it's preceded by a dollar sign (e.g. $1, $2, ...
3
votes
2answers
452 views

Replace a pattern in a 2d array with another pattern

I'm trying to write a method that does the following: Replace every occurrence of the array A with the array B, where A is inside the 2D array C, then return the modified array. A, B, and C are ...
2
votes
4answers
192 views

How to replace special character and its next string with one string

How to replace the special character $owner with "hr" and $table_name with "hr" and $constraint_name with "scma_constraint" in dynamic. Change "alter table $owner.$table_name ENABLE constraint ...
0
votes
3answers
174 views

Convert a string to another one in java [closed]

How do I convert #title to <h1>title</h1> in java? I'm trying to create an algorithm to convert markdown format to html format.
0
votes
4answers
6k views

Using string.replace() in Java [duplicate]

Possible Duplicate: Hints for java.lang.String.replace problem? I have a sentence that is passed in as a string and I am doing a replace on the word "and" and I want to replace it with " ". ...
-1
votes
4answers
745 views

Java 1.4.2's replace() takes in chars only, how can I get a char value of \n and \r\n carriage returns?

Java 1.4.2's replace() takes in chars only. How can I get a char value of \n and \r\n carriage returns? \n = Unix carriage return \r\n = Windows carriage return
-6
votes
4answers
313 views

If user inputs random letter, how do I change all of that letter in a given string?

I am trying to figure out how to use character wrapping to mutate a string based on user input. If string is 'Bob loves to build building' and user enters 'b' I have to make the out put change both ...
8
votes
6answers
5k views

Ignoring diacritic characters when comparing words with special caracters (é,è,…)

I have a list with some Belgian cities with diacritic characters: (Liège, Quiévrain, Franière, etc.) and I would like to transform these special characters to compare with a list containing the same ...
3
votes
2answers
528 views

Java: Regex replacing

I have this String: foo bar 567 baz Now I want to add before each number the String num:. So the result has to be: foo bar num:567 baz Also this has to work: foo 73761 barbazboom!! 87 result: ...
7
votes
2answers
252 views

Is there a regular expression way to replace a set of characters with another set (like shell tr command)?

The shell tr command support replace one set of characters with another set. For example, echo hello | tr [a-z] [A-Z] will tranlate hello to HELLO. In java, however, I must replace each character ...
7
votes
5answers
9k views

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: ...
5
votes
6answers
8k views

Faster alternatives to replace method in a Java String?

The fact that the replace method returns a string object rather than replacing the contents of a given string is a little obtuse (but understandable when you know that strings are immutable in Java). ...
4
votes
5answers
2k views

String.replaceAll is considerably slower than doing the job yourself

I have an old piece of code that performs find and replace of tokens within a string. It receives a map of from and to pairs, iterates over them and for each of those pairs, iterates over the target ...
3
votes
5answers
13k views

Java: Find and replace words/lines in a file

I have a file (more specifically, a log4j configuration file) and I want to be able to read in the file and pick out certain lines in the code and replace them. For example, within the file there is a ...
2
votes
3answers
2k views

Replacing multiple substrings in Java when replacement text overlaps search text

Say you have the following string: cat dog fish dog fish cat You want to replace all cats with dogs, all dogs with fish, and all fish with cats. Intuitively, the expected result: dog fish cat fish ...
1
vote
1answer
2k views

How to escape a single quote from within a JSP?

Im totally stuck on this, Im trying to escape a single quote in a JSP. I have some data that Im outputting directly into a JS string and the single quotes seem to be causing issues. Here is my code: ...
1
vote
3answers
3k views

Modifying existing file content in Java

I want to replace the second line file content, can somebody help please based on the below file format and listener method. 1324254875443 1313131 Paid 0.0 2nd line is long and want to replace to ...
1
vote
1answer
197 views

Intelli-J structural search and replace problems

Is there an easy way to capture types? I can't seem to do basic things like use variable expressions side by side, such as $mapType$$mapEnd$ to do a simple replacement. Is there any reason this might ...
0
votes
4answers
7k views

Regex for special characters in java

public static final String specialChars1= "\\W\\S"; String str2 = str1.replaceAll(specialChars1, "").replace(" ", "+"); public static final String specialChars2 = ...

1 2
15 30 50 per page