Replace Last Occurrence of a String in a String

Replace the last occurrence of a string in another string

Find the index of the last occurrence of the substring.

String myWord = "AAAAAasdas";
String toReplace = "AA";
String replacement = "BBB";

int start = myWord.lastIndexOf(toReplace);

Create a StringBuilder (you can just concatenate Strings if you wanted to).

StringBuilder builder = new StringBuilder();

Append the part before the last occurrence.

builder.append(myWord.substring(0, start));

Append the String you want to use as a replacement.

builder.append(replacement);

Append the part after the last occurrence from the original `String.

builder.append(myWord.substring(start + toReplace.length()));

And you're done.

System.out.println(builder);

Replace last occurrence of a string in a string

You can use this function:

function str_lreplace($search, $replace, $subject)
{
$pos = strrpos($subject, $search);

if($pos !== false)
{
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}

return $subject;
}

Remove last occurrence of a string in a string

The String.LastIndexOf method does what you need - returns the last index of a char or string.

If you're sure that every string will have at least one set of parentheses:

var result = node.Text.Substring(0, node.Text.LastIndexOf("("));

Otherwise, you could test the result of LastIndexOf:

var lastParenSet = node.Text.LastIndexOf("(");

var result =
node.Text.Substring(0, lastParenSet > -1 ? lastParenSet : node.Text.Count());

Replace last occurrence of character in string

You don't need jQuery, just a regular expression.

This will remove the last underscore:





var str = 'a_b_c';

console.log( str.replace(/_([^_]*)$/, '$1') ) //a_bc

Replace Last Occurrence of a character in a string


String str = "\"Position, fix, dial\"";
int ind = str.lastIndexOf("\"");
if( ind>=0 )
str = new StringBuilder(str).replace(ind, ind+1,"\\\"").toString();
System.out.println(str);

Update

 if( ind>=0 )
str = new StringBuilder(str.length()+1)
.append(str, 0, ind)
.append('\\')
.append(str, ind, str.length())
.toString();

JavaScript: replace last occurrence of text in a string

Well, if the string really ends with the pattern, you could do this:

str = str.replace(new RegExp(list[i] + '$'), 'finish');

rreplace - How to replace the last occurrence of an expression in a string?


>>> def rreplace(s, old, new, occurrence):
... li = s.rsplit(old, occurrence)
... return new.join(li)
...
>>> s
'1232425'
>>> rreplace(s, '2', ' ', 2)
'123 4 5'
>>> rreplace(s, '2', ' ', 3)
'1 3 4 5'
>>> rreplace(s, '2', ' ', 4)
'1 3 4 5'
>>> rreplace(s, '2', ' ', 0)
'1232425'

Replacing last occurrence of substring in string

Regular Expressions can also perform this task. Here is an example of one that would work. It will replace the last occurrence of "Aquarius" with "Bumblebee Joe"

$text = "This is the dawning of the age of Aquarius. The age of Aquarius, Aquarius, Aquarius, Aquarius, Aquarius"
$text -replace "(.*)Aquarius(.*)", '$1Bumblebee Joe$2'
This is the dawning of the age of Aquarius. The age of Aquarius, Aquarius, Aquarius, Aquarius, Bumblebee Joe

The greedy quantifier ensure that it take everything it can up until the last match of Aquarius. The $1 and $2 represent the data before and after that match.

If you are using a variable for the replacement you need to use double quotes and escape the $ for the regex replacements so PowerShell does not try to treat them as a variable

$replace = "Bumblebee Joe"
$text -replace "(.*)Aquarius(.*)", "`$1$replace`$2"

Replace the last occurrence of a word in a string - C#

Here is the function to replace the last occurrence of a string

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
int place = Source.LastIndexOf(Find);

if(place == -1)
return Source;

string result = Source.Remove(place, Find.Length).Insert(place, Replace);
return result;
}
  • Source is the string on which you want to do the operation.
  • Find is the string that you want to replace.
  • Replace is the string that you want to replace it with.


Related Topics



Leave a reply



Submit