开发者

String manipulation, replacing " " with (space)

开发者 https://www.devze.com 2023-04-04 10:42 出处:网络
I\'ve been working on a function that changes the spaces between words into the string \" \"(space).

I've been working on a function that changes the spaces between words into the string " "(space).

For example, "Hello World. Hi there." would become "Hello(space)world.(space)Hi(space)there."

EDIT: I'm trying to build this to a specific set of structured English which is as follows:

  • set the initial value of result to an empty string
  • for each index in the argument string
  • if the character at that index is a space then
  • append '(space)' to result
  • else
  • append the character at that index to result
  • end if
  • end for
  • return result

Here is what I was able to come up with so far.:

function showSpaces(aString)
{
var word, letter;

word = aString
for var (count = 0; count < word.length; count = count + 1)

{
    letter = word.charAt(count);
    if (letter == " ")
    {
        return("(space)");
    }
    else
    {
        return(letter);
    }
}
}

Wheneve开发者_开发百科r I test this function call, nothing happens:

<INPUT TYPE = "button" NAME = "showSpacesButton"  VALUE ="Show spaces in a string as (space)"
        ONCLICK = "window.alert(showSpaces('Space: the final frontier'));">

I'm just beginning with JavaScript at the moment. Any help would be appreciated.

-Ross.


Use String.replace

function showSpaces(aString)
{
    return aString.replace(/ /g,'(space)');
}

EDIT: to get your code working:

function showSpaces (aString)
{

    var word, letter,
        output = ""; // Add an output string

    word = aString;
    for (var count = 0; count < word.length; count = count + 1) // removed var after for

    {
        letter = word.charAt(count);
        if (letter == " ")
        {
            output += ("(space)"); // don't return, but build the string
        }
        else
        {
            output += (letter); // don't return, but build the string
        }
    }
    return output; // once the string has been build, return it

}


No, "nothing" doesn't happen. It very rarely does. What happens is that you are getting a syntax error in the code because you used for var ( instead of for (var.

If you fix that, you will notice that you only get the first character in the string, as you use return inside the loop instead of putting together a string and returning it after the loop.

You can do like this:

function showSpaces(word) {
  var letter, result = "";
  for (var count = 0; count < word.length; count++) {
    letter = word.charAt(count);
    if (letter == " ") {
      result += "(space)";
    } else {
      result += letter;
    }
  }
  return result;
}

Demo: http://jsfiddle.net/Guffa/pFkhs/

(Note: Using += to concatenate strings performs badly for long strings.)

You can also use a regular expression to replace a string:

function showSpaces(word) {
  return word.replace(/ /g, "(space)");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消