Replace the Last Part of a String

Replace the last part of a string

The following code should replace the last occurrence of a ',' with a ')'.

StringBuilder b = new StringBuilder(yourString);
b.replace(yourString.lastIndexOf(","), yourString.lastIndexOf(",") + 1, ")" );
yourString = b.toString();

Note This will throw Exceptions if the String doesn't contain a ','.

Replace last part of string, javascript

You could create a regular expression from the string and preserve the dot by escaping it and use the end marker of the string to replace only the last occurence.

const    replace = (string, pattern) => string.replace(new RegExp(pattern.replace(/\./g, '\\\$&') + '$'), '')
console.log(replace(".1.2.1", ".1"));console.log(replace("CABC", "C"));

Replace last character of string using JavaScript

You can do it with regex easily,

var str1 = "Notion,Data,Identity,".replace(/.$/,".")

.$ will match any character at the end of a string.

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

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

Replace last part of a string

You can use:

LEFT (Viewsmap, LEN(Viewsmap) - CHARINDEX('\', REVERSE(Viewsmap)) + 1 )

in order to extract the part that is before the last occurrence of \.

You can then easily UPDATEusing:

UPDATE mytable
SET Viewsmap = CONCAT(LEFT(Viewsmap, LEN(Viewsmap) - CHARINDEX('\', REVERSE(Viewsmap)) + 1 ),
'newPath')
WHERE PathName like '%1%'
OR PathName like '%2%'
OR PathName like '%3%'

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

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;
}


Related Topics



Leave a reply



Submit