Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop string each word

if this type character '這' = NonEnglish each will take up 2 word space, and English will take up 1 word space, Max length limit is 10 word space; How to get the first 10 space.
for below example how to get the result This這 is?
I'm trying to use for loop from first word but I don't know how to get each word in string...

string = "This這 is是 English中文 …";

var NonEnglish = "[^\u0000-\u0080]+",
    Pattern = new RegExp(NonEnglish),
    MaxLength = 10,
    Ratio = 2;
like image 477
user1775888 Avatar asked Feb 27 '14 05:02

user1775888


People also ask

How do you split a string and iterate in Python?

Not directly splitting strings as such, but the re module has re. finditer() (and corresponding finditer() method on any compiled regular expression). An example of how to use re. finditer() to iterate split strings would be helpful.

How do you loop through a string?

For loops are used when you know you want to visit every character. For loops with strings usually start at 0 and use the string's length() for the ending condition to step through the string character by character. String s = "example"; // loop through the string from 0 to length for(int i=0; i < s.

How do you get all the words out of a string in Python?

Method #1 : Using split() Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.


1 Answers

If you mean you want to get that part of the string where it's length has reached 10, here's the answer:

var string = "This這 is是 English中文 …";

function check(string){
  // Length of A-Za-z characters is 1, and other characters which OP wants is 2
  var length = i = 0, len = string.length; 

  // you can iterate over strings just as like arrays
  for(;i < len; i++){

    // if the character is what the OP wants, add 2, else 1
    length += /\u0000-\u0080/.test(string[i]) ? 2 : 1;

    // if length is >= 10, come out of loop
    if(length >= 10) break;
  }

  // return string from the first letter till the index where we aborted the for loop
  return string.substr(0, i);
}

alert(check(string));

Live Demo

EDIT 1:

  1. Replaced .match with .test. The former returns a whole array while the latter simply returns true or false.
  2. Improved RegEx. Since we are checking only one character, no need for ^ and + that were before.
  3. Replaced len with string.length. Here's why.
like image 169
HighBoots Avatar answered Oct 14 '22 18:10

HighBoots