• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

Javascript: SPLIT input string but retain deg symbol

I'm taking a line of input from an external file and parsing the comma delimited data using the SPLIT command as follows:

               var strLatLon = content.split(",")[10];       // Decimal LatLon

The command retrieves the expected data from the input string, but I am losing the degrees symbol from the data.  The following is an example of the LatLon I am trying to capture:

               "N 42° 45.103;W 71° 29.167"    (data is quoted in the input file, I do want to collect the LatLon as a pair as a single variable)

When I display the value:  alert (strLatLon), I see a BLOCK in the position of the degrees symbol.

               "N 42� 43.743;W 71� 26.850"

Can someone please suggest an approach which will preserve (or restore) the degrees symbol in this string?



Thanks!

0
markp99
Asked:
markp99
  • 5
  • 4
  • 2
  • +2
1 Solution
 
Alkali_GuyCommented:
alert(unescape(strLatLon));
0
 
MasoudghCommented:
hi,
you can use unescape function which decodes an encoded string like this (strange characters you indicate)
this type of statements can create by another function like escape.

so your code must change like this:
alert unescape((strLatLon));
0
 
MasoudghCommented:
for more information check here : http://www.w3schools.com/jsref/jsref_unescape.asp
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
markp99Author Commented:
Thanks for the suggestion.

Neither form presents the degrees symbol:

     unescape(strLatLon);
     unescape((strLatLon));

At home I see a "block" character,  here at work I see a "?" in place of the degrees symbol.

The input file (.txt) shows the degrees symbol properly.



Any additional thoughts?
0
 
MasoudghCommented:
yeah I don't check "�", this code convert to a block which you said. I think it's better to copy the complete code you used.
0
 
MasoudghCommented:
I found something new, I think you use a browser like FIREFOX at your work and use something like IE at your home. "�" in firefox show as a diamond with question mark and in IE show as a block. But I think the complete code may help us to solve the problem.
0
 
mark-bCommented:
Did you try setting the character set encoding of your html page to be the same as the text file you are loading from?

For example, set the charset on the page to use iso-8859-1:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
       <script language="JavaScript1.2">
         var latLong = "latLong "; // load the latLong in this variable from the file
        
         alert( latLong );
       </script>  
      </head>
      <body></body>
</html>

Don't know if this will work, since I can't repeat the problem.  But it may be worth a shot.
0
 
markp99Author Commented:
mark-b,

I was hopeful your sugestions would have solved my problem, but I get the same bad result.

Your snippet of code is basically what I am attempting, except with a reference to an external file which I step thru line-by-line to grab the comma-delimited string.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script language="JavaScript1.2">

       <!read the string from the external file>
       <!SPLIT the string to parse data elements>

        var latLong = "latLong "; // load the latLong in this variable from the file
        alert( latLong );

     </script>  
     </head>
     <body></body>
</html>

The following is a typical line from that external file:

String = -71.45935,42.762917,GCWJFA,"Riverfront Promenade","String Theory",T,2.0,1.0,Micro,N,N 42° 45.775;W 71° 27.561

The problem data element is at the END of thae string at comma position #10 (the deg symbol was present when I posted this msg, but will be probably be escaped when you read this).


Thanks for your help!
0
 
mark-bCommented:
Hmm.  Well, I'm not sure how you are loading the data, but this works in Firefox:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <script language="JavaScript1.2">
         var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
         if ( window.XMLHttpRequest ) xhr.overrideMimeType("text/plain; charset=ISO-8859-1");
         xhr.open("GET", "data.txt", true);
         xhr.onreadystatechange=function() {
           if (xhr.readyState==4) {
             var latLong=xhr.responseText;
             alert( latLong.split(",")[10] );
           }
        }
        xhr.send(null);
      <script>  
     </head>
     <body></body>
</html>

My data file for this example is a file called 'data.txt' and it only has that one line in your example.  The problem with my example is does not work in IE because the XMLHttpRequest ActiveX object in IE does not support he overrideMimeType method.  IE 7 might, but IE 6 does not.

How are you loading the text file?

-Mark
0
 
markp99Author Commented:
Mark,

Here is basic idea:

var page=[];
page["geoFile"] = "GSAK.txt";

parseFile = function(geoString) {
      infoW = geoString.split("\n");
      for (var i=0; i<infoW.length; i++) {
            var content = infoW[i];
            if (content.indexOf(",") != -1) {
                  var lat = content.split(",")[1]*1;              
                  var lng = content.split(",")[0]*1;        
                  ....collect more variables...
                  var strLatLon = content.split(",")[10];  
            ...do some more stuff...
            alert(strLatLon);
            }
      }
}

function mungeFile(iso){
      GDownloadUrl(page[iso],parseFile);
}

mungeFile('geoFile');  //This call used in a number of locations in the js file



BTW, this is a Google Maps app.


Thanks for your assistance!
0
 
markp99Author Commented:
I am using IE 6 & 7, but would also like to support Firefox.
0
 
markp99Author Commented:
I found a bit of a brute force solution:

I replaced "°" with a valid ascii character "^" when I export from my database to the .txt input file. When I get the data into .js, I simply replace "^" back to "°" and all is well.

A nice simple solution to a bugger of a problem.


I'll call this my solution and will move on.  :)


Thanks for the effort, Masoudgh & mark-b
0
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now