简体   繁体   中英

How do I use .split() to break up a string into much smaller strings?

I am making a code that is supposed to split up the alphabet into each letter on a different line. When I press the submit button, instead of displaying the letters, it does absolutely nothing at all. I am not sure why it is not working.

The output should be:
A:
B:
C:
D:
etc...

Any help is appreciated, but please do not use JQuery.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns:hi="http://www.w3.org/1999/xhtml"> <head> <title>My String Prototype</title> </head> <body> <form action = "#"> <input type="submit" id = "sub" value="Submit" onclick = "show_alphabet();"/> </form> <script type="text/javascript"> function show_alphabet () { var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; str.prototype.sendArray= function (){ str.split(""); }; var arr = str.sendArray(); for (var i=0;i>arr.length;i++){ document.writeln(arr+":"); } } </script> </body> </html> 

THANKS!

There's no need to split the string into characters if you can already do that by default:

 var string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; for(var i = 0; i < string.length; i++){ document.getElementById('results').innerHTML += '\\n' + string[i]; } 
 <pre id="results"></pre> 

A few issues:

  1. Need to use String.prototype.sendArray =
  2. Need to return a value
  3. The i > arr.length will never be true
  4. Need to use arr[i] to access the current element. arr is the entire array

This appears to work as expected:

function show_alphabet () {
    var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String.prototype.sendArray = function (){
        return str.split("");
    };
    var arr = str.sendArray();
    for (var i = 0; i < arr.length; i++){
        document.write(arr[i] + ":");
    }
}

There are these issues in your code:

  1. Your sendArray() method needs to return its result.
  2. You need to use String.prototype.sendArray = function() {...} , not str.prototype.sendArray = ... .
  3. When you create a new method on the prototype, you refer to the current instance as this , not str .
  4. You have to reference the array item with arr[i] .
  5. Your comparison operation in your for statement is backwards.

I don't see why you're creating a new method that does nothing more than .split() , but if you want to do that, then you need to return the new value and use this instead of str and use String.prototype :

    String.prototype.sendArray= function (){
        return this.split("");
    };

But, you don't really need that new method at all. Here's a working snippet with those above items fixed/changed:

  function show_alphabet () { var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var arr = str.split(""); for (var i=0;i < arr.length;i++){ document.write(arr[i] + "<br>"); console.log(arr[i]); } } show_alphabet(); 

Or, using your sendArray() method:

  String.prototype.sendArray= function (){ return this.split(""); }; function show_alphabet () { var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var arr = str.sendArray(); for (var i=0;i < arr.length;i++){ document.write(arr[i] + "<br>"); console.log(arr[i]); } } show_alphabet(); 


Prototype Usage Explanation

Here's a bit of explanation about the .prototype issue. The .prototype property is a direct property of a constructor function, not an individual object. That means it's a property of String , not str . Once you change it on String , then all objects created from that constructor will automatically inherit that change. So, when you add something to String.prototype , it will automatically be available on str as a direct method.

Then, when that method is invoked, the value of this will point to the current object so to reference a method or property on the current object from a prototyped method, you use this as the base. So, in this case, you would call this.split("") so split the current string.

Since this looks like homework, you don't get the answer outright.

1) When you split a string, you pick which character to split on. Ex: Splitting ("AB") (A then space then B). Returns an array with 2 elements. A & B.

2) You don't need all the string prototype stuff like you see in online help. var arr = str.split(... works fine.

3) Your greater than sign should be less than in for loop. You wnat to repeat while i is less than the length, not greater.

4) You don't want to writeln arr. You want to writeln arr[i], or the i'th element of the array.

5) To do the newline in HTML replace ":" with " <br /> "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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