Questions tagged [replace]
Replacing is the action of searching a string for a sub-string and replacing it with a different string.
replace
30,906
questions
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', ...
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']
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 ...
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?
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 ...
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?
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"
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....
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 "...
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/...
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 ...
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?
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:...
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-...
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 ...
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 ...
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, ...
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-...
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 ...
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:
...
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 ...
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?
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", "")....
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"
...
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 ...
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?
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 = ...
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 ...
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 ...
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(...
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, ...
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 ...
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 ...
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"...
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
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 ...
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 ...
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?
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.
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 ...
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.
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?
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....
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 ...
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");
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.
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 ...
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 ...
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 ...
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.