remove string until . in typescript

To remove everything before the first occurrence of a '.' character in a given string in TypeScript, you can use the substring() method and combine it with the indexOf() method.

Here is an example function that implements this logic:

index.ts
function removeStringUntilDot(str: string): string {
  const dotIndex = str.indexOf('.');
  if (dotIndex !== -1) {
    return str.substring(dotIndex + 1);
  }
  return str;
}
175 chars
8 lines

In this function, we first find the index of the first '.' character using indexOf(). If the dot is found, we use substring() to extract the substring starting from the index immediately after the '.' character. Otherwise, we simply return the original string.

Here is an example usage:

index.ts
const input = 'hello.world';
const output = removeStringUntilDot(input); // 'world'
84 chars
3 lines

Note that this function only removes the first occurrence of a '.' character. If there are multiple '.' characters in the input string, only the part after the first one will be returned. If you need to remove everything after the last '.' character, you can use lastIndexOf() instead of indexOf().

gistlibby LogSnag