1

Assume we have such strings.

const del = 'Deleted'
const str1 = 'clean.Deleted'
const str2 = 'get.clean.Deleted'
const str3 = 'cl.Deleted'

And I need return every time str1, str2,str3 without .Deleted

It is work for me:

any_string.substr(0, (any_string.length - del.length-1))

Do we have a more generic way?

2 Answers 2

3

If .Deleted is always .Deleted, then string.replace

const newString = oldString.replace('.Deleted', '')

You can replace that with RegExp if you only want .Deleted that happens at the end.

const newString = oldString.replace(/\.Deleted$/, '')
Sign up to request clarification or add additional context in comments.

Comments

1

To achieve expected result, use slice with negative value for slicing from last(.Deleted length is 8, so use -8)

str.slice('.Deleted',-8)

JS:

const del = 'Deleted'
const str1 = 'clean.Deleted'
const str2 = 'get.clean.Deleted'
const str3 = 'cl.Deleted'

console.log(str1.slice('.Deleted',-8))
console.log(str2.slice('.Deleted',-8))
console.log(str3.slice('.Deleted',-8))

Note: This solution works only if the .Deleted is always at the last part of the string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.