I have a very small query. I tried using concat, charAt, slice and whatnot but I didn't get how to do it.
Here is my string:
var str1 = "Notion,Data,Identity,"
I want to replace the last ,
with a .
it should look like this.
var str1 = "Notion,Data,Identity."
Can someone let me know how to achieve this?
To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str. length - 1) .
Answer: Use the JavaScript replace() method You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.
Find the index of the last occurrence of the substring. String myWord = "AAAAAasdas"; String toReplace = "AA"; String replacement = "BBB"; int start = myWord. lastIndexOf(toReplace);
To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.
You can do it with regex easily,
var str1 = "Notion,Data,Identity,".replace(/.$/,".")
.$
will match any character at the end of a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With