简体   繁体   English

如何使用javascript从起始索引到空白字符提取字符串?

[英]How to extract string from starting index to white space character using javascript?

I'm new to programming and would like to extract a string from certain an index up to a whitespace character. 我是编程新手,想从某个索引中提取一个字符串,直到一个空白字符。

Consider string "hello world from user" 考虑字符串"hello world from user"

and the cursor position is at index 6 . 光标位置在索引6 From index 6, I want to extract string the string up until the whitespace character so the output would be "world" . 从索引6,我想提取字符串直到空白字符,以便输出为"world" How can I achieve this? 我该如何实现?

I have tried using: 我试过使用:

cursor_position = event.target.selectionStart;
extracted_string = event.target.value.substr(cursor_position, 
event.target.value.indexOf(' '));

But this second position for extracting string doesn't seem to be correct. 但是提取字符串的第二个位置似乎不正确。 Could someone help me with extracting the string from the cursor's position to the white space character? 有人可以帮我从光标位置提取字符串到空白字符吗?

Thanks. 谢谢。

First you need to get the string from the cursor position to the end of the string. 首先,您需要从光标位置到字符串末尾获取字符串。 Afterwards you can chain another .substr() call to trim the string from the beginning to the first occurence of a whitespace. 之后,您可以链接另一个.substr()调用,以从开始到第一次出现空白处修剪字符串。 Here's an example: 这是一个例子:

 var str = "hello world from user"; var cursorPosition = 6; str = str.substr(cursorPosition, str.length).substr(0, str.indexOf(' ')); console.log(str); 

You can use .slice() to cut the string from your starting index to the end of the word, and then use .split() on your new string to "chunk" it into an array where each element is a word separated from the string separated by a space. 您可以使用.slice()将字符串从起始索引切到单词的末尾,然后对新字符串使用.split()将其“分块”成一个数组,其中每个元素都是一个单词,与字符串,以空格分隔。

Eg: 例如:

"hello world from user" --> slice(6) --> "world from user"

Then: 然后:

"world from user" --> split(' ') --> ["world", "from", "user"]

Getting the first element/word (index 0 ) from the split array will give "word" 从分割数组中获取第一个元素/单词(索引0 )将得到"word"

See example below: 请参见下面的示例:

 const str = "hello world from user"; const idx = 6; const res = str.slice(idx).trim().split(' ')[0]; console.log(res); // "world" 

If you need it such that when you start on a space you get the next word, you can use .trim() before you .split() the array: 如果需要它,那么当从空格开始时会得到下一个单词,可以在数组.split()之前使用.trim()

 const str = "hello world from user"; const idx = 5; const res = str.slice(idx).trim().split(' ')[0]; console.log(res); // "world" 

You can achieve it this way 你可以这样实现

cursor_position = event.target.selectionStart;
extracted_string = event.target.value.substr(cursor_position);
next_word_length = extracted_string.split(' ')[0].length
next_word = event.target.value.substr(cursor_position, next_word_length)

indexOf takes fromIndex as a second argument. indexOffromIndex作为第二个参数。 So no need to have all those chainings. 因此,不需要所有这些链接。 You can simply use the function below. 您可以简单地使用下面的功能。

const extract = (str, startIndex, search = " ") => str.slice(startIndex, str.indexOf(search, startIndex));

const myString = extract("hello world from user", 6);
console.log(myString);

// Output: "world"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM