简体   繁体   English

如何返回从指定字符开始的字符串? Javascript

[英]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.

相关问题 如何使用javascript从起始索引到空白字符提取字符串? - How to extract string from starting index to white space character using javascript? 如何在JavaScript中将整数更改为具有指定字符集的字符串 - How to change an integer into a string with specified character set in JavaScript 如何定义字符串开头不存在的字符? - How to define a character is not there at starting of string? Javascript:提取字符串中以特定字符开头的单词 - Javascript : Extract words starting with specific character in a string JavaScript-从位置到行尾/返回字符获取字符串 - JavaScript - Get string from position till end of line / return character 如何在JAVAScript中比较字符串并忽略句子中的起始字符串 - How to compare the string and ignore the starting string from a sentence in JAVAScript 如何从javascript中的字符串中提取参数(指定的模式字符串) - how to extract parameter(specified pattern string) from string in javascript 如何在 JavaScript 中获取指定字符之前的子字符串? - How to grab substring before a specified character in JavaScript? 匹配以 : 字符开头但不以 \\ 开头的字符串: - Match a string starting with : character, but not starting with \: 如何使用从特定字符开始的 JavaScript 将字符串拆分为单词,然后在下一个空格处结束? - How can I split a string into words with JavaScript starting at a particular character, and then end at the next space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM