Questions tagged [replace]

Replacing is the action of searching a string for a sub-string and replacing it with a different string.

Replacing is the action of searching a string (the haystack) for a sub-string (the needle) and replacing it with a different string.

for example, replacing all 'l' within 'Hello' with 'y', would result in 'Heyyo'.

27903 questions
336
votes
32 answers

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

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, except I also want the one or more spaces at the…
Nessa
  • 3,369
  • 2
  • 16
  • 3
333
votes
6 answers

How can I replace a character with a newline in Emacs?

I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs. I have tried the following commands: M-x replace-string RET ; RET \n This will replace ; with two characters: \n. M-x replace-regex…
Lei
  • 3,381
  • 3
  • 18
  • 7
332
votes
7 answers

Replace specific characters within strings

I would like to remove specific characters from strings within a vector, similar to the Find and Replace feature in Excel. Here are the data I start with: group <- data.frame(c("12357e", "12575e", "197e18", "e18947") I start with just the first…
Luke
  • 4,769
  • 5
  • 28
  • 36
326
votes
10 answers

How can I add a string to the end of each line in Vim?

I want to add * to the end of each line in Vim. I tried the code unsuccessfully :%s/\n/*\n/g
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
325
votes
28 answers

Replace multiple strings with multiple other strings

I'm trying to replace multiple words in a string with multiple other words. The string is "I have a cat, a dog, and a goat." However, this does not produce "I have a dog, a goat, and a cat", but instead it produces "I have a cat, a cat, and a cat".…
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
321
votes
8 answers

Find and replace with sed in directory and sub directories

I run this command to find and replace all occurrences of 'apple' with 'orange' in all files in root of my site: find ./ -exec sed -i 's/apple/orange/g' {} \; But it doesn't go through sub directories. What is wrong with this command? Here are some…
hd.
  • 17,596
  • 46
  • 115
  • 165
316
votes
13 answers

Difference between String replace() and replaceAll()

What's the difference between java.lang.String 's replace() and replaceAll() methods, other than later uses regex? For simple substitutions like, replace . with / , is there any difference?
Jijoy
  • 12,386
  • 14
  • 41
  • 48
313
votes
16 answers

Is there an alternative to string.Replace that is case-insensitive?

I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount% with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace() method. I've seen…
Aheho
  • 12,622
  • 13
  • 54
  • 83
309
votes
7 answers

Replace one character with another in Bash

I need to replace a space ( ) with a dot (.) in a string in bash. I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
299
votes
7 answers

MySQL search and replace some text in a field

What MySQL query will do a text search and replace in one particular field in a table? I.e. search for foo and replace with bar so a record with a field with the value hello foo becomes hello bar.
julz
  • 3,369
  • 2
  • 17
  • 8
291
votes
8 answers

Strip Leading and Trailing Spaces From Java String

Is there a convenience method to strip any leading or trailing spaces from a Java String? Something like: String myString = " keep this "; String stripppedString = myString.strip(); System.out.println("no spaces:" + strippedString); Result: no…
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196
288
votes
4 answers

postgresql - replace all instances of a string within text field

In postgresql, how do I replace all instances of a string within a database column? Say I want to replace all instances of cat with dog, for example. What's the best way to do this?
mark
  • 10,316
  • 6
  • 37
  • 58
273
votes
8 answers

Replace all elements of NumPy array that are greater than some value

I have a 2D NumPy array. How do I replace all values in it greater than a threshold T = 255 with a value x = 255? A slow for-loop based method would be: # arr = arr.copy() # Optionally, do not modify original arr. for i in range(arr.shape[0]): …
NLi10Me
  • 3,182
  • 2
  • 13
  • 15
262
votes
18 answers

Replace part of a string with another string

How do I replace part of a string with another string using the standard C++ libraries? QString s("hello $name"); // Example using Qt. s.replace("$name", "Somename");
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
250
votes
14 answers

Replace a string in a file with nodejs

I use the md5 grunt task to generate MD5 filenames. Now I want to rename the sources in the HTML file with the new filename in the callback of the task. I wonder what's the easiest way to do this.
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297