EzDevInfo.com

replace interview questions

Top replace frequently asked interview questions

Renaming columns in pandas

I have a data table 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 data table A where the original column names are:

['$a', '$b', '$c', '$d', '$e'] 

to

['a', 'b', 'c', 'd', 'e'].

I have the edited column names stored it in a list, but I don't know how to replace the column names.


Source: (StackOverflow)

Awk/Sed: How to do a recursive find/replace of a string?

How to I find and replace every occurrence of:

subdomainA.example.com

with

subdomainB.example.com

in every text file under the /home/www/ directory tree (recursive find/replace).


Source: (StackOverflow)

Advertisements

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");

Source: (StackOverflow)

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?


Source: (StackOverflow)

Update a column value, replacing part of a string

I have a table with the following columns in a MySQL database

[id, url]

And the urls are like:

 http://domain1.com/images/img1.jpg

I want to update all the urls to another domain

 http://domain2.com/otherfolder/img1.jpg

keeping the name of the file as is.

What's the query must I run?


Source: (StackOverflow)

Replace a Space with a Period in Bash

All I need to be able to do is 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.


Source: (StackOverflow)

Javascript how to remove text from a string

I've got a string that has 'data-123' as its value. How in jQuery or Javascript would I go in and remove the 'data-' from the string while leaving the '123'?


Source: (StackOverflow)

How do I perform a Perl substitution on a string while keeping the original?

In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original?

I usually just copy the string to a new variable then bind it to the s/// regex that does the replacement on the new string, but I was wondering if there is a better way to do this?

$newstring = $oldstring;
$newstring =~ s/foo/bar/g;

Source: (StackOverflow)

Is there an alternative to string.Replace that is case-insensitive?

I need to search a string and replace all occurances of %FirstName% and %PolicyAmount% with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace() method. I've seen web pages on the subject that suggest

Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);

However for some reason when I try and replace %PolicyAmount% with $0, the replacement never takes place. I assume that it has something to do with the dollar sign being a reserved character in regex.

Is there another method I can use that doesn't involve sanitizing the input to deal with regex special characters?


Source: (StackOverflow)

Regular expression search replace in Sublime Text 2

I'm looking to do search replace on 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 term:

Find what: my name is (\w)+

Replace with: my name used to be $(1)

The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.


Source: (StackOverflow)

Strip Leading and Trailing Spaces From Java String [duplicate]

Possible Duplicate:
trim whitespace from a 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.println("no spaces:" + strippedString);

Result:

no spaces:keep this

myString.replace(" ","") would replace the space between keep and this.

Thanks


Source: (StackOverflow)

How can I replace two strings in a way that one does not end up replacing the other?

Let's say that I have the following code:

String word1 = "bar";
String word2 = "foo";
String story = "Once upon a time, there was a foo and a bar."
story = story.replace("foo", word1);
story = story.replace("bar", word2);

After this code runs, the value of story will be "Once upon a time, there was a foo and a foo."

A similar issue occurs if I replaced them in the opposite order:

String word1 = "bar";
String word2 = "foo";
String story = "Once upon a time, there was a foo and a bar."
story = story.replace("bar", word2);
story = story.replace("foo", word1);

The value of story will be "Once upon a time, there was a bar and a bar."

My goal is to turn story into "Once upon a time, there was a bar and a foo." How could I accomplish that?


Source: (StackOverflow)

How to 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 replace ; with 2 characters: \n.

  • M-x replace-regex RET ; RET \n

    This results in the following error (shown in the minibuffer):

    Invalid use of `\' in replacement text.

What's wrong with using replace-string for this task? Is there any other way to do it?

Thanks.


Source: (StackOverflow)

Visual studio find and replace - Add carriage return, OR Newline

In the case of following string to be parsed.

ford mustang,10,blue~~?bugatti veyron,13,black

I want to replace the ~~? with a carriage return

Replacing with \n just adds the string "\n"

I'm sure this can be done?

Thanks


Source: (StackOverflow)

VIM Replace word with contents of paste buffer?

I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g. I know that this is the typical way one replaces the word at the current cursor position: cw<text><esc> but is there a way to do this with the contents of the unnamed register as the replacement text and without overwriting the register?


Source: (StackOverflow)