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'.

24721 questions
4813
votes
72 answers

How to replace all occurrences of a string in JavaScript

I have this string in my JavaScript code: "Test abc test test abc test test test abc test test abc" Doing: str = str.replace('abc', ''); Seems to only remove the first occurrence of abc in the string above. How can I replace all occurrences of it?
Click Upvote
  • 235,452
  • 251
  • 553
  • 736
2216
votes
29 answers

Renaming columns in Pandas

I have a DataFrame using Pandas and column labels that I need to edit to replace the original column labels. I'd like to change the column names in a DataFrame A where the original column names are: ['$a', '$b', '$c', '$d', '$e'] to ['a', 'b', 'c',…
user1504276
  • 22,263
  • 3
  • 12
  • 7
2128
votes
11 answers

How to replace a character by a newline in Vim

I'm trying to replace each , in the current file by a new line: :%s/,/\n/g But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything. What should I do? If you are curious, like me, check the question…
Vinko Vrsalovic
  • 244,143
  • 49
  • 315
  • 361
759
votes
35 answers

How to do a recursive find/replace of a string with awk or sed?

How do I find and replace every occurrence of: subdomainA.example.com with subdomainB.example.com in every text file under the /home/www/ directory tree recursively?
Tedd
  • 7,635
  • 3
  • 15
  • 5
720
votes
14 answers

Fastest method to replace all instances of a character in a string

What is the fastest way to replace all instances of a string/character in a string in JavaScript? A while, a for-loop, a regular expression?
Anriëtte Myburgh
  • 12,127
  • 11
  • 47
  • 71
629
votes
24 answers

How do I replace a character at a particular index in JavaScript?

I have a string, let's say Hello world and I need to replace the char at index 3. How can I replace a char by specifying a index? var str = "hello world"; I need something like str.replaceAt(0,"h");
Santhosh
  • 16,978
  • 21
  • 60
  • 74
621
votes
14 answers

Find and Replace Inside a Text File from a Bash Command

What's the simplest way to do a find and replace for a given input string, say abc, and replace with another string, say XYZ in file /tmp/file.txt? I am writting an app and using IronPython to execute commands through SSH — but I don't know Unix…
Ash
  • 21,088
  • 34
  • 100
  • 145
586
votes
5 answers

MySQL string replace

I have a column containing urls (id,…
n00b
  • 14,958
  • 19
  • 52
  • 71
502
votes
10 answers

UPDATE and REPLACE part of a string

I've got a table with two columns, ID and Value. I want to change a part of some strings in the second column. Example of Table: ID Value --------------------------------- 1 c:\temp\123\abc\111 2 …
aston_zh
  • 5,483
  • 3
  • 16
  • 22
498
votes
29 answers

In Vim is there a way to delete without putting text in the register?

Using Vim I often want to replace a block of code with a block that I just yanked. But when I delete the block of code that is to be replaced, that block itself goes into the register which erases the block I just yanked. So I've got in the habit of…
Edward Tanguay
  • 176,854
  • 291
  • 683
  • 1,015
473
votes
4 answers

Python string.replace regular expression

I have a parameter file of the form: parameter-name parameter-value Where the parameters may be in any order but there is only one parameter per line. I want to replace one parameter's parameter-value with a new value. I am using a line replace…
Troy Rockwood
  • 5,225
  • 3
  • 13
  • 20
467
votes
6 answers

Regular expression search replace in Sublime Text 2

I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so something like converting this text: Hello my name is bob And this search…
hackerhasid
  • 11,151
  • 9
  • 40
  • 60
448
votes
18 answers

How to replace all dots in a string using JavaScript

I want to replace all the occurrences of a dot(.) in a JavaScript string For example, I have: var mystring = 'okay.this.is.a.string'; I want to get: okay this is a string. So far I tried: mystring.replace(/./g,' ') but this ends up with all the…
Omar Abid
  • 14,746
  • 24
  • 72
  • 105
432
votes
18 answers

How can I remove a character from a string using JavaScript?

I am so close to getting this, but it just isn't right. All I would like to do is remove the character r from a string. The problem is, there is more than one instance of r in the string. However, it is always the character at index 4 (so the 5th…
user1293504
  • 4,361
  • 2
  • 13
  • 6
416
votes
8 answers

Replace a character at a specific index in a string?

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 an error. Is there any method to do this?
dpp
  • 25,478
  • 28
  • 95
  • 150
1
2 3
99 100