-1

I've this:

"example: myresult"

And I need a method that looks for this from ": "

"myresult"

I tried with String.search()

David Thomas
  • 249,100
  • 51
  • 377
  • 410
NBG
  • 11
  • 1
  • How reliable is the format of the string? If you know it's always going to have a colon and single white space separating them, then you can just use `.split`. e.g. `"example: myresult".split(': ')[1]` will give you "myresult" – Jayce444 Nov 10 '22 at 11:18
  • 1
    What kind of result are you expecting? – Ivar Nov 10 '22 at 11:19
  • Split method works, thanks – NBG Nov 10 '22 at 11:56

5 Answers5

1

use string.includes(searchString, position), that have a second param of position to start the search.

position Optional The position within the string at which to begin searching for searchString. (Defaults to 0.)

const str = "example: myresult";
const startIndex = str.indexOf(': ');
console.log(str.includes('example')); // true
console.log(str.includes('example', startIndex)); // false
console.log(str.includes('myresult', startIndex)); //true
Evgeni Dikerman
  • 486
  • 3
  • 18
0

this should work

function getSecondPart(str) {
    return str.split(':')[1];
}
Naveen
  • 356
  • 2
  • 10
0

As a last resort you have also the regular expression route to extract the value following semicolons preceded by a label:

const subject = "example: myresult";
const re = /^.+?\: (.+)$/im;
const match = re.exec(subject);
let result = "";
if (match !== null) {
    result = match[1];  
}

console.log(result);
Diego D
  • 6,156
  • 2
  • 17
  • 30
  • "last resort" indeed – Evgeni Dikerman Nov 10 '22 at 11:26
  • it was an unfortunate way to introduce my answer as just the nth option among many.. and the less appropriate indeed. On the other hand it scales better in case you mean to make changes to the parsing rules at any time.. but that's just an added consideration – Diego D Nov 10 '22 at 11:28
  • well it's hard to tell if it will scale better, dynamic regex over dynamic string known to be less efficient https://stackoverflow.com/a/5296314/1761692 – Evgeni Dikerman Nov 10 '22 at 11:33
  • 1
    by scaling I meant how much effort is required to change the logic in case the syntax changes. The regex is easy to change if your parsing rules get different. Any other strategy like using split or tricks to get the semicolon position, will require to reconsider the whole block. Of course, performance wise, the regex is the less efficient way to deal with that problem... that's why I said _the less appropriate (option)_. Anyway I didn't mean to win the competition just cite an added option. That's all. – Diego D Nov 10 '22 at 12:27
0

Assuming the ": " is always consistent this should work for you:

var myresult = "example: myresult";
console.log(myresult.split(': ')[1]);
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
solarroi
  • 13
  • 5
-1

try

'string'.includes(':')

you can check if the symbol or letter is in the word by includes in the words

BugsNRoses
  • 11
  • 4