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'
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
Finding last occurrence of substring in string, replacing that
This should do it
old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]
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);
To replace but the last occurrence of string in a text
str.replace()
method has a count
argument:
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Then, use str.count()
to check how many and
in the string and then -1
(because you need the last and
):
str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring sub in the range
[start, end]
. Optional arguments start and end are interpreted as in slice notation.
Demo:
>>> string = 'Saturday and Sunday and Monday and Tuesday and Wednesday and Thursday and Friday are days of the week.'
>>> string.replace(' and ', ", ", (string.count(' and ')-1))
'Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday and Friday are days of the week. '
How to replace the last occurrence of a part of a search string in Python
You can use a group ()
and a backreference \1
.
>>> mystring = "a, b, c, d"
>>> print(re.sub(r", (\w)$", r" & \1", mystring))
a, b, c & d
Reference
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;
}
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;
return Source.Remove(place, Find.Length).Insert(place, Replace);
}
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.
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');
Related Topics
How to Combine Two Lists into a Dictionary in Python
How to Tell If a String Repeats Itself in Python
Difference Between Parsing a Text File in R and Rb Mode
Why Can't I Use the Method _Cmp_ in Python 3 as for Python 2
Time Complexity of Accessing a Python Dict
Send Data from a Textbox into Flask
How to Access a Dictionary Key Value Present Inside a List
Adding Labels in X Y Scatter Plot with Seaborn
Is There a Simple Way to Change a Column of Yes/No to 1/0 in a Pandas Dataframe
How to Get 'Real-Time' Information Back from a Subprocess.Popen in Python (2.5)
How to Write String Literals in Python Without Having to Escape Them
Python (And Python C API): _New_ Versus _Init_
Using Lxml and Iterparse() to Parse a Big (+- 1Gb) Xml File
Converting "Yield From" Statement to Python 2.7 Code
How to Merge Two Dataframes Side-By-Side