Re: problem with split method



Daniel Kirsch wrote:
Roy - Topup Communications wrote:
Hi, I have to check strings in a text area, there are spaces between each string and I used split method to sort the array. anyhow,it doesn't work with spaces, only with (,). how can i work it out?

Works for me:
alert("this is a test".split(" ").length)

If you want to recreate the string from the sorted array, use the array's join method and pass the character you want as parameter:

alert("this is a test".split(" ").sort().join(" "));

You may create a small function to do the job and some checkings:

function sortString(str,delimiter) {
if (typeof str != "string") return str;
if (typeof delimiter != "string") delimiter = " ";
return str.split(delimiter).sort().join(delimiter);
}

Daniel
.



Relevant Pages

  • sort 1-D array of doubles for Olaf Schmidt
    ... A few weeks Olaf Schmidt posted a VB6 routine to sort a 1-D array of strings very fast indeed. ... fastest way to change case of string. ... Dim i As Long, j As Long, Lo As Long, Hi As Long, StPtr As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Collection Structure
    ... property StringID you want ... where inarr is the String array and outarr is an array of longs containing ... needs to sort the input array keeping track of the original position (I use ...
    (microsoft.public.vb.com)
  • Re: About word frequency
    ... Use the split function to turn the string into an array. ... Sort the array. ... For each sLine in aList ...
    (microsoft.public.scripting.vbscript)
  • Re: Quick method to sort a list of strings?
    ... 'split string into an array around so each line is a seperate item ... the cell may represent the number of purchases by ... I do not have all the these comments stored in an array ... separate entities and sort them in alpha-numeric order? ...
    (microsoft.public.excel.programming)
  • RE: Quick method to sort a list of strings?
    ... 'split string into an array around so each line is a seperate item ... the cell may represent the number of purchases by ... I do not have all the these comments stored in an array ... separate entities and sort them in alpha-numeric order? ...
    (microsoft.public.excel.programming)