--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - strchr('Hello, World!', 'o') - strchr('Hello', 'x') returns: - '''o, World!''' - 'null' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) notes: - |- Returns a pointer to the first occurrence of c in str. In JS, returns the substring starting from the first occurrence, or null. parityVerified: null isTypescript: true type: function layout: function title: C's string.strchr in TypeScript description: >- Here's what our current TypeScript equivalent to C's strchr found in the string.h header file looks like. function: strchr category: string language: c permalink: c/string/strchr/ alias: - /functions/c/strchr/ - /functions/string/strchr/ - /c/strchr/ ---
{% codeblock lang:typescript %}export function strchr(str: string, c: string): string | null { // discuss at: https://locutus.io/c/string/strchr/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns a pointer to the first occurrence of c in str. // note 1: In JS, returns the substring starting from the first occurrence, or null. // example 1: strchr('Hello, World!', 'o') // returns 1: 'o, World!' // example 2: strchr('Hello', 'x') // returns 2: null str = str + '' c = (c + '').charAt(0) const idx = str.indexOf(c) return idx === -1 ? null : str.slice(idx) } {% endcodeblock %}