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
Read Each Line of Txt File to New Array Element
Yii2 Htaccess - How to Hide Frontend/Web and Backend/Web Completely
File_Get_Contents(): PHP_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data
Switch PHP Versions on Commandline Ubuntu 16.04
PHP: Check If an Array Contains All Array Values from Another Array
Symfony2 - Force File Download
Fatal Error: Call to Undefined Function Mb_Detect_Encoding()
Show a Pdf Files in Users Browser Via PHP/Perl
PHP: If Internet Explorer 6, 7, 8 , or 9
What Is the Default Lifetime of a Session
How to Store File Name in Database, With Other Info While Uploading Image to Server Using PHP
PHP Get Values from Json Encode
How to Write to the Console in PHP