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
5404
votes
78 answers

How do I replace all occurrences of a string in JavaScript?

Given a string: s = "Test abc test test abc test test test abc test test abc"; This seems to only remove the first occurrence of abc in the string above: s = s.replace('abc', ''); How do I replace all occurrences of it?
Ali
  • 261,656
  • 265
  • 575
  • 769
2886
votes
36 answers

Renaming column names in Pandas

I want to change the column labels of a Pandas DataFrame from ['$a', '$b', '$c', '$d', '$e'] to ['a', 'b', 'c', 'd', 'e']
user1504276
  • 28,955
  • 3
  • 15
  • 7
2370
votes
12 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
  • 330,807
  • 53
  • 334
  • 373
906
votes
37 answers

How can I 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
  • 9,105
  • 3
  • 17
  • 5
802
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
  • 13,347
  • 11
  • 51
  • 72
770
votes
18 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
  • 24,276
  • 34
  • 107
  • 152
765
votes
9 answers

Remove a fixed prefix/suffix from a string in Bash

I want to remove the prefix/suffix from a string. For example, given: string="hello-world" prefix="hell" suffix="ld" How do I get the following result? "o-wor"
Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
754
votes
30 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
  • 19,616
  • 22
  • 63
  • 74
707
votes
26 answers

Remove specific characters from a string in Python

I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately, it appears to do nothing to the string. for char in line: if char in " ?.!/;:": line.replace(char,'') How do I do…
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
635
votes
6 answers

MySQL string replace

I have a column containing urls (id,…
n00b
  • 16,088
  • 21
  • 56
  • 72
598
votes
9 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
  • 6,543
  • 4
  • 20
  • 23
589
votes
12 answers

How do I find and replace all occurrences (in all files) in Visual Studio Code?

I can't figure out how to find and replace all occurrences of a word in different files using Visual Studio Code version 1.0. I get the impression this should be possible since doing Ctrl + Shift + F allows me to simply search a folder, but i am…
Cisum Inas
  • 11,552
  • 11
  • 40
  • 55
538
votes
30 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
  • 189,012
  • 314
  • 712
  • 1,047
535
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,875
  • 3
  • 15
  • 20
517
votes
22 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
  • 5,211
  • 2
  • 15
  • 6
1
2 3
99 100