[英]How to return a string starting from specified character? Javascript
I have to create a function that searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str.我必须创建一个 function 来搜索参数 str 指向的字符串中第一次出现的字符 c(无符号字符)。 The terminating null character is considered to be part of the string.
终止字符 null 被认为是字符串的一部分。
So i did this:所以我这样做了:
function my_strchr(str, c){
return str.substring(c.length + str.indexOf(c));
}
But it returns a string without c character.但它返回一个没有 c 字符的字符串。 For example, input is "abcabc" && b.
例如,输入是“abcabc”&& b。 Expected output is bcabc, but I have cabc.
预期 output 是 bcabc,但我有 cabc。 Could anybody help?
有人可以帮忙吗?
make it like this and it should be okay让它像这样应该没问题
return str.substring(str.indexOf(c));
it will start from the index of C till the end of str
它将从 C 的索引开始,直到
str
结束
check查看
https://www.w3schools.com/jsref/jsref_substring.asp
https://www.w3schools.com/jsref/jsref_substring.asp
to know more about the substring()
了解更多关于
substring()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.