跳过字符串中的空格

Skip whitespacing from string

本文关键字:空格 字符串      更新时间:2024-05-15

如何在我的小脚本中跳过空白计数和着色?它在我的剧本中被计算在内,并被着色,我没有线索如何跳过它,而不是影响角色。所以重点是每两个字母都涂上颜色。有什么建议吗?

我知道如何使用jQuery,但我正在练习JavaScript。

JS Fiddle演示

var div = document.createElement('div'),
    first = 'First JavaScript string.',
    second = 'This is second text.',
    combine = first + ' ' + second,
    colored = '';
div.id = 'sort-text';
document.getElementsByTagName('body')[0].appendChild(div);
for (i = 0; i < combine.length; i++) {
    if (i % 2 == 1) {
        colored += '<span style="color: #d10;">' + combine[i] + '</span>';
    } else {
        colored += combine[i];
    }
}
div.innerHTML = colored;

我建议您使用map。对于这个问题,它比for循环要干净得多。

例如:

var curr = -1;
colored = combine.split("").map(function(x) {
    if (x === " ") return x; // Ignore space
    curr++; // Otherwise, increment
    if (curr % 2 === 1)
      return '<span style="color: #d10;">' + x + '</span>';
    return x;
}).join("");

您可以很容易地将其扩展为包括其他标点符号和模式。

例如,如果我们想同时忽略句点和空格,我们可以这样做:

var curr = -1;
var ignore = ". "; // Ignore space and period
colored = combine.split("").map(function(x) {
    if (ignore.indexOf(x) >= 0) return x;
    curr++;
    if (curr % 2 === 1)
      return '<span style="color: #d10;">' + x + '</span>';
    return x;
}).join("");

FIDDLE

试试这个:

var space_offset = 0;
for (i = 0; i < combine.length; i++) {
    if ( combine[i] == ' ' ) {
        space_offset++;
        colored += combine[i];
    } else if ( ( i + space_offset ) % 2 == 1) {
        colored += '<span style="color: #d10;">' + combine[i] + '</span>';
    } else {
        colored += combine[i];
    }
}

我添加了一个偏移量变量,它将保持忽略空格的其他字母都应该着色的想法。

您可以将不想计数的东西列为白名单,比如空格,并使用单独的变量,而不是依赖于字符索引。

var useRed = false;
for (i = 0; i < combine.length; i++) {
    if (combine[i] != ' ') {
        if (useRed) {
            colored += '<span style="color: #d10;">' + combine[i] + '</span>';
            useRed = false;
        } else {
            colored += combine[i];
            useRed = true
        }
    }
    else {
        colored += combine[i];
    }
}

这是小提琴。

似乎是continue:的任务

var i, len, str, bol, curr_char, new_str;
str = "hello wolrd this is a test.";
new_str = "";
bol = false;
for(i=0, len=str.length; i<len; i++){
    curr_char = str.charAt(i);
    if(curr_char===" "){
        new_str += curr_char;
        continue;
    }
    if(bol){
        new_str += "<span style='color: #d10;'>" + curr_char + "</span>";
    }else{
        new_str += curr_char;
    }
    bol = !bol;
}
console.log(new_str);

这是另一个选项:

var i, len, old_str, new_str, color_toggler, current_char, is_space;
old_str = "hello world this is a test.";
new_str = "";
color_toggler = true;
for(i=0, len=old_str.length; i<len; i++){
    current_char = old_str.charAt(i);
    is_space = (current_char===" ");
    if(!is_space){
        color_toggler = !color_toggler;
    }
    if(color_toggler || is_space){
        new_str += current_char;
    }else{
        new_str += "<span style='color: #d10;'>" + current_char + "</span>";
    }
}
console.log(new_str);