Skip to main content

Questions tagged [replace]

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

Filter by
Sorted by
Tagged with
5473 votes
50 answers
4.6m views

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

Given a string: string = "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: string = string.replace('abc', ...
Ali's user avatar
  • 266k
2993 votes
33 answers
6.6m views

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's user avatar
2441 votes
12 answers
1.1m views

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 ...
Vinko Vrsalovic's user avatar
  • 337k
936 votes
37 answers
796k views

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's user avatar
  • 9,405
830 votes
18 answers
980k views

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 writing an app and using IronPython to execute ...
Ash's user avatar
  • 25.4k
812 votes
14 answers
1.0m views

Fastest method to replace all instances of a character in a string [duplicate]

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's user avatar
810 votes
9 answers
767k views

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ý's user avatar
780 votes
30 answers
967k views

How do I replace a character at a specific 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 replaced a char by specifying an index? var str = "hello world"; I need something like str....
Santhosh's user avatar
  • 20.2k
720 votes
27 answers
1.9m views

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 "...
Matt Phillips's user avatar
651 votes
6 answers
837k views

MySQL string replace

I have a column containing urls (id, url): http://www.example.com/articles/updates/43 http://www.example.com/articles/updates/866 http://www.example.com/articles/updates/323 http://www.example.com/...
n00b's user avatar
  • 16.4k
643 votes
12 answers
777k views

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 ...
Cisum Inas's user avatar
  • 12.5k
641 votes
19 answers
755k views

How to replace all occurrences of a character in string?

What is the effective way to replace all occurrences of a character with another character in std::string?
big-z's user avatar
  • 7,042
620 votes
9 answers
1.3m views

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:...
aston_zh's user avatar
  • 6,763
553 votes
4 answers
1.2m views

Python string.replace regular expression [duplicate]

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-...
Troy Rockwood's user avatar
551 votes
30 answers
95k views

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 ...
Edward Tanguay's user avatar
527 votes
23 answers
1.5m views

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 ...
user1293504's user avatar
  • 5,311
524 votes
30 answers
506k views

How to trim a file extension from a String in JavaScript?

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, ...
ma11hew28's user avatar
  • 125k
516 votes
7 answers
551k views

How to input a regex in string.replace?

I need some help on declaring a regex. My inputs are like the following: this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-...
alvas's user avatar
  • 120k
497 votes
6 answers
386k views

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 ...
asbestossupply's user avatar
487 votes
17 answers
638k views

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: ...
Omar Abid's user avatar
  • 16k
482 votes
26 answers
1.4m views

How can I replace text with CSS?

How can I replace text with CSS using a method like this: .pvw-title img[src*="IKON.img"] { visibility:hidden; } Instead of ( img[src*="IKON.img"] ), I need to use something that can replace text ...
MokiTa's user avatar
  • 5,010
479 votes
9 answers
1.2m views

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?
kazinix's user avatar
  • 29.9k
472 votes
28 answers
837k views

How to replace multiple substrings of a string?

I would like to use the .replace function to replace multiple strings. I currently have string.replace("condition1", "") but would like to have something like string.replace("condition1", "")....
CQM's user avatar
  • 43.7k
430 votes
3 answers
865k views

JavaScript - Replace all commas in a string [duplicate]

I have a string with multiple commas, and the string replace method will only change the first one: var mystring = "this,is,a,test" mystring.replace(",","newchar", -1) Result: "thisnewcharis,a,test" ...
mike's user avatar
  • 23.5k
424 votes
11 answers
1.4m views

Finding and replacing elements in a list

I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this? For example, suppose my ...
James's user avatar
  • 4,267
381 votes
9 answers
620k views

How to use string.replace() in python 3.x

The string.replace() is deprecated on python 3.x. What is the new way of doing this?
Dewsworld's user avatar
  • 13.9k
376 votes
16 answers
832k views

Best way to replace multiple characters in a string?

I need to replace some characters as follows: & ➔ \&, # ➔ \#, ... I coded as follows, but I guess there should be some better way. Any hints? strs = strs.replace('&', '\&') strs = ...
prosseek's user avatar
  • 188k
371 votes
7 answers
327k views

Replace spaces with dashes and make all letters lower-case

I need to reformat a string using jQuery or vanilla JavaScript Let’s say we have "Sonic Free Games". I want to convert it to "sonic-free-games". So whitespaces should be replaced by dashes and all ...
EzzDev's user avatar
  • 10.2k
364 votes
10 answers
548k views

How to search and replace using grep

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string. I know that the command to find it might look ...
billtian's user avatar
  • 6,719
360 votes
22 answers
1.0m views

How to search and replace text in a file

How do I search and replace text in a file using Python 3? Here is my code: import os import sys import fileinput print("Text to search for:") textToSearch = input("> ") print(...
Shriram's user avatar
  • 4,851
347 votes
32 answers
498k views

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, ...
Nessa's user avatar
  • 3,479
347 votes
6 answers
132k views

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 ...
Lei's user avatar
  • 3,521
341 votes
13 answers
483k views

How to swap text based on patterns at once with sed?

Suppose I have 'abbc' string and I want to replace: ab -> bc bc -> ab If I try two replaces the result is not what I want: echo 'abbc' | sed 's/ab/bc/g;s/bc/ab/g' abab So what sed command can I ...
DaniloNC's user avatar
  • 3,585
341 votes
8 answers
776k views

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"...
Luke's user avatar
  • 4,889
338 votes
10 answers
390k views

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 준영's user avatar
334 votes
28 answers
485k views

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 ...
Anderson Green's user avatar
327 votes
8 answers
385k views

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 ...
hd.'s user avatar
  • 18k
324 votes
13 answers
355k views

Difference between String replace() and replaceAll()

What's the difference between java.lang.String 's replace() and replaceAll() methods, other than the latter uses regex? For simple substitutions like, replace . with /, is there any difference?
Jijoy's user avatar
  • 12.6k
317 votes
7 answers
374k views

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's user avatar
315 votes
16 answers
130k views

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 ...
Aheho's user avatar
  • 12.8k
303 votes
6 answers
252k views

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's user avatar
  • 3,389
296 votes
4 answers
302k views

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's user avatar
  • 10.5k
290 votes
8 answers
445k views

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....
Tyler DeWitt's user avatar
  • 23.5k
280 votes
8 answers
618k views

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 ...
NLi10Me's user avatar
  • 3,252
278 votes
19 answers
484k views

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's user avatar
  • 19.6k
267 votes
15 answers
319k views

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's user avatar
261 votes
7 answers
53k views

Visual Studio Clicking Find Results Opens Code in Wrong Window

I'm using Visual Studio 2010 and when I do a "Find in Files" the results are returned to the "Find Results 1" window which is docked below my code editor window. Before, I would double click on one ...
JPM's user avatar
  • 2,611
251 votes
7 answers
685k views

How to replace text in a string column of a Pandas dataframe?

I have a column in my dataframe like this: range "(2,30)" "(50,290)" "(400,1000)" ... and I want to replace the , comma with - dash. I'm currently using this method but ...
UserYmY's user avatar
  • 8,424
244 votes
9 answers
177k views

Replace only some groups with Regex

Let's suppose I have the following regex: -(\d+)- and I want to replace, using C#, the Group 1 (\d+) with AA, to obtain: -AA- Now I'm replacing it using: var text = "example-123-example"; var ...
Oscar Mederos's user avatar
242 votes
11 answers
535k views

How to remove all characters after a specific character in python?

I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one.
Solihull's user avatar
  • 7,049

1
2 3 4 5
619