Home > Software engineering >  How to slice part of string from the middle to the end using javascript
How to slice part of string from the middle to the end using javascript

Time:01-24

I have string like this:

река Марица - гр. Първомай   Maritsa - 132543 - HPoint-Water level

I want to get only Maritsa - 132543 - HPoint-Water level with javascript.

For now I get the string and console log all string, but I need to cut the cyrillic string and - before Maritsa. The problem is that the loaded names are always different. Is there a way to delete everything before the including it and show everything after the

image

CodePudding user response:

You could replace the unwanted part.

const s = 'река Марица - гр. Първомай   Maritsa - 132543 - HPoint-Water level';

console.log(s.replace(/.*\ \s?/, ''));

CodePudding user response:

You can combine String.prototype.indexOf (which returns the first occurence of a char) and Array.prototype.slice (to return the string only after a certain index, here after the and the space after it) :

const name = 'река Марица - гр. Първомай   Maritsa - 132543 - HPoint-Water level'

const cutString = string => string.slice(string.indexOf(' ')   2)
//           add 2 to also remove the   and the space after it ^ 

console.log(`"${ cutString(name) }"`)

Hope it helped you !

  • Related