Questions tagged [character-replacement]
72 questions
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
42
votes
10 answers
Replace multiple strings in one gsub() or chartr() statement in R?
I have a string variable containing alphabet[a-z], space[ ], and apostrophe['],eg. x <- "a'b c"
I want to replace apostrophe['] with blank[], and replace space[ ] with underscore[_].
x <- gsub("'", "", x)
x <- gsub(" ", "_", x)
It works absolutely,…

Eric Chang
- 2,580
- 4
- 19
- 19
6
votes
5 answers
Character replacement in strings in VB.NET
How fast can I replace characters in a string?
So the background on this question is this: We have a couple of applications that communicate with each other and with clients' applications through sockets. These socket messages contain non-printable…

Tim
- 222
- 1
- 2
- 9
6
votes
3 answers
How to do Byte Pair Encoding bigram counting and replacements efficiently in Python?
In the Byte Pair Encoding algorithm, there's a replacement step where it changes the character strings delimited by spaces to bigrams.
I.e., given a list of str tuples as such:
[('t', 'h', 'i', 's', '\ue000'), ('c', 'o', 'r', 'p', 'u', 's',…

alvas
- 115,346
- 109
- 446
- 738
6
votes
3 answers
How to replace particular character in string with tcl script?
set some_string "Name/is/ComplexSTRUCTUre"
convert this string to,
some_string = "Name/is/ComplexSTR.CTUre"
i.e replacing first "U" to "."

Harshad_Dinfi
- 89
- 1
- 1
- 6
6
votes
4 answers
Show comma in CSV without using the comma character
I have a log in CSV format we write out for a certain logging operation. However, one of the fields allows user input and I need to make sure that if they enter a comma in the field that we parse it out and replace it with something that, say, Excel…

codewario
- 19,553
- 20
- 90
- 159
4
votes
2 answers
Pythonic way to replace chars
I want to replace some characters in a string using a pythonic approach.
A -> T
C -> G
G -> C
T -> A
Example:
AAATCGATTGAT
will transform into
TTTAGCTAACTA
What I did:
def swap(string):
string = re.sub('A', 'aux', string)
string =…

lmalmeida
- 135
- 2
- 14
4
votes
2 answers
Powershell, replace DOT with SPACE
This works:
$string = "This string, has a, lot, of commas in, it."
$string -replace ',',''
Output: This string has a lot of commas in it.
But this doesn't work:
$string = "This string. has a. lot. of dots in. it."
$string -replace…
user6496229
4
votes
7 answers
Converting space to "+" using C#
I want to convert a string to an url and, instead of a space, it needs a "+" between the keywords.
For instance:
"Hello I am"
to:
"Hello+I+am"
How should i do this?

klopske
- 83
- 1
- 5
3
votes
2 answers
Can quantifiers be used in regex replacement in R?
My objective would be replacing a string by a symbol repeated as many characters as have the string, in a way as one can replace letters to capital letters with \\U\\1, if my pattern was "...(*)..." my replacement for what is captured by (*) would…

iago
- 2,990
- 4
- 21
- 27
3
votes
1 answer
Using mgsub function with word boundaries for replacement values
I am trying to replace substrings of string elements within a vector with blank spaces. Below are the vectors we are considering:
test <- c("PALMA DE MALLORCA", "THE RICH AND THE POOR", "A CAMEL IN THE DESERT", "SANTANDER SL", "LA")
lista <-…

MN Beitelmal
- 165
- 8
2
votes
3 answers
Replacing numerical values in a FASTA file with their index in a different file. (Bash preferred)
I have a folder full of fasta files with the following format (and so on), where the line beginning with > is the read name of DNA sequence, and the following line is the sequence itself. This pattern repeats for the entire file:
>…

jelfman
- 23
- 4
2
votes
3 answers
What is an easier way to replace multiple characters with other characters in a string in swift?
I am currently trying to setup a string to be added to a HTTP POST request where a user would type text and tap 'enter' and a request would be sent.
I know multiple characters (^,+,<,>) can be replaced with a single character ('_') like…

MrPickles2009
- 117
- 4
- 10
2
votes
1 answer
Visual Studio 2008: String Literals "??-", "??'", "??=" corrupt
Recently I came across a bug in Visual Studio 2008 (at least I think it is one).
When I try to create string-literals with two questionmarks followed by another character, something weird occurs: Those three chars are replaced by another…

jaap-de-boer
- 137
- 1
- 1
- 8
2
votes
1 answer
tr output to new lines
I am trying to remove all non-alphanumeric from a text file with the following command:
cat textfile.txt | tr -cd [:alnum:] > textfile_alnum.txt
This works, except that now the output within textfile_alnum.txt is all on the same line. How can I…

Alkthree
- 133
- 2
- 7