Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi!

I have a javascript string which is the result of Ajax async operation.

code:
var xmlHttp_obj = new XmlHttpRequest( );

 xmlHttp_obj.onreadystatechange = function( ) {
                                 if ( xmlHttp_obj.readyState == 4) // ready 
                                {
                                  var cities_List = xmlHttp_obj.responseText;
                                  // what to do NEXT??
                                 
                                }
                              }

 xmlHttp_obj.open("POST", "http://localhost/" + window.location.host + "/TestASP_NET/AsyncTest.ashx", true);

xmlHttp_obj.send(null);


The incoming string contains cities sperated by a linebreak (\n)

How do I get each city from a list?
What regex?
Posted
Updated 27-Mar-11 13:18pm
v7
Comments
Albin Abel 27-Mar-11 11:50am    
What is the string format your are getting? is it comma separated?
Nick Reshetinsky 27-Mar-11 11:53am    
on a server side it's done like that

StringBuilder sb = new StringBuilder( );

while(reader.Read( )) {
sb.AppenLine( reader.GetString(0)); // \n (linbreak)
}
this.context.Response.Output.WriteLine( sb.ToString( ));
Sandeep Mewara 27-Mar-11 12:23pm    
What's your ultimate goal here? Get cities and then? Further as Albie asked, each city is separated by what? Format?
Nick Reshetinsky 27-Mar-11 12:51pm    
linebreak; (\n) symbol
Bryian Tan 27-Mar-11 17:06pm    
hello,

How about var mySplitCity = cities_List.split("\n");
Then loop through the mySplitCity array to display the cities.

1 solution

Parse the string based on whatever separator is between the cities?? Seriously, this is just a simple string manipulation. Unfortunately for you, you didn't supply any information on what format the string comes back in, so there's little to nothing anyone can tell you about what you have to do.
 
Share this answer
 
v2
Comments
Nick Reshetinsky 27-Mar-11 11:55am    
linebreaks are inserted between words on the server (ASP.NET)
Dave Kreskowiak 27-Mar-11 13:47pm    
In that case, all you have to do is string.Split on ControlCharacters.CrLf to get an array of those cities.
Sergey Alexandrovich Kryukov 27-Mar-11 16:33pm    
Indian "spelling accent" fixed. Is it infecting you? :-)
--SA
Dave Kreskowiak 27-Mar-11 18:03pm    
It's called a "typo", not an accent.
Sergey Alexandrovich Kryukov 27-Mar-11 19:05pm    
For that case I reserved my smile sign :-)
I called it accent because in very many question this spellings is strongly correlated with the native culture of the writer. I know such things well because such think happens with my native cultures. In your case, it was my joke. :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900