Home » excel » Is there a way to use the Find and Replace function in Excel to replace the first and last entry in a string of text?

Is there a way to use the Find and Replace function in Excel to replace the first and last entry in a string of text?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a sequence of a letters, in my case part of a gene. I want to change the first and the last letter in this string of text, but keep the internal characters the same.

For example, if I have the the sequence:

  • ATCGAATCCATGACG

And I want to change the first letter, in this case A to the word START and change the last letter, G, to STOP all while keeping the internal A’s and G’s the same. Is this possible to do with the Find and Replace function, or will I have to write a script?

It is easy to do when I have a handful of sequences, I do it by hand. When I get into the hundreds, it can be very difficult.

Thank you.

How to&Answers:

The function LEN(text) returns the number of characters within a string of letters. MID(text, start, num_chars) returns the middle section of a string. CONCATENATE(text1, text2, ...) pieces together different strings. We can use these in combination to get what you want:

=CONCATENATE("START", MID(A1,2,LEN(A1)-2), "STOP")

enter image description here

Answer:

You could use replace, and focus on the left and right side independently, then combine, or you can use left/right to add string of text to the available string minus a character, like:

="START"&LEFT(RIGHT(A1,LEN(A1)-1),LEN(A1)-2)&"STOP"

enter image description here

I used left/right, but mid would also work

Answer:

just another option:

=REPLACE(REPLACE(A1,LEN(A1),1,"STOP"),1,1,"START")

enter image description here