简体   繁体   中英

String.split("¤"); not working

So, I've got this task from school that drives me crazy. I am going to take data from a .dat file which contains this:

812¤SuperIT¤2015-12-06 18:00¤25

614¤MediaHuset¤2016-01-14 16:15¤67

My script works if I replace the "¤" with, for example, ";", but it doesn't work with "¤", " ¤ ", " ¤ "

window.onload = start;

var xhttp;

function start() {
    document.getElementById('sub').onclick = load;
}

function load() {
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = statusChange;
    xhttp.open('GET', '../presentasjoner.dat', true);   //GET or POST
    xhttp.send();

}
function statusChange() {
    if (xhttp.readyState === 4 && xhttp.status === 200) {
        var content = xhttp.responseText;
        var lines = content.split("\n");

        document.getElementById('table').innerHTML = "";
        for (var i = 0; i < lines.length; i++) {
            var parts = lines[i].split('&curren;');

            document.getElementById('table').innerHTML +=
            "<h4>" + parts[0] + "</h4>" +
            parts[1] + "<br/>" +
            "Antall plasser: " + parts[2] + "<br/>";
        }
    }
}

The HTML looks like this:

<!DOCTYPE html>
<html> 
    <head>
        <meta charset="UTF-8">
        <title>Oppgave 1 - Oblig 5</title>
        <link rel="stylesheet" href="../css/common.css">
        <link rel="stylesheet" href="../css/common.css">
        <script type="text/javascript" src="../js/oppg1.js"></script>
    </head>

    <body>
        <div class="commonDiv">
            <a href="http://ask.hiof.no/~joakimsg/GRIT/WEB/html/fanpage.html"><h1>Oblig 4, Joakim Granaas</h1></a><br/>
            <h3>Oppgave 1</h3><br/>
            <input id ="sub" type ="submit" /><br/>
            <div   id="table"></div>
        </div>
    </body>
</html>

¤ is probably just a nonprintable character. Use od -c to find out what it actually is, and split on that.

$ echo 'abc' | od -c
0000000   a   b   c  \n
0000004

Splitting by "¤" def. works, see here: https://jsfiddle.net/s7t0c5jg/ --- so the issue is either elsewhere in your code OR your browser has a hard time processing the "¤" or you are saving your file as UTF8 or something and "¤" could be a higher character than just UTF8?

After reading the comments above I think Axel is correct in saying "Usually ¤ just only indicates a non printable character and it does not mean that this char is actually within your data. Open the data file with an hex editor and have a look at the very separator ascii value"

JSFiddle source:

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">split by "¤"</button>
<p id="result"></p>
<script>
function myFunction() {
    var str = "Item1 ¤ Item2 ¤ Item3 ¤ Item4";
    var res = str.split("¤");
    document.getElementById("result").innerHTML = res;
}
</script>
</body>
</html>

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