slice string until . in typescript

You can use the indexOf function to find the first occurrence of a period (.) in the string, and then use the substr function to extract the substring from the beginning of the original string up to (but not including) the period.

index.ts
const str = 'hello.world';
const index = str.indexOf('.');
const result = str.substr(0, index);
console.log(result); // "hello"
128 chars
5 lines

Alternatively, you can use the substring function instead of substr.

index.ts
const str = 'hello.world';
const index = str.indexOf('.');
const result = str.substring(0, index);
console.log(result); // "hello"
131 chars
5 lines

gistlibby LogSnag