String.split("¤");不工作
String.split("¤"); not working
所以,我从学校接到了这个让我发疯的任务。我将从包含以下内容的 .dat 文件中获取数据:
812¤SuperIT¤2015-12-06 18:00¤25
614¤MediaHuset¤2016-01-14 16:15¤67
如果我将“¤”替换为“;”,我的脚本就可以运行,但它不适用于“¤”、“¤
”、“¤
”
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('¤');
document.getElementById('table').innerHTML +=
"<h4>" + parts[0] + "</h4>" +
parts[1] + "<br/>" +
"Antall plasser: " + parts[2] + "<br/>";
}
}
}
HTML 看起来像这样:
<!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>
¤
可能只是一个不可打印的字符。使用 od -c
找出它到底是什么,然后拆分。
$ echo 'abc' | od -c
0000000 a b c \n
0000004
按“¤”def 拆分。有效,请参见此处:https://jsfiddle.net/s7t0c5jg/ --- 所以问题出在您的代码中的其他地方,或者您的浏览器很难处理“¤”,或者您将文件保存为 UTF8 或其他格式,“¤”可能是比 UTF8 更高的字符?
看了上面的评论我觉得Axel说的是对的 "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 来源:
<!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>
所以,我从学校接到了这个让我发疯的任务。我将从包含以下内容的 .dat 文件中获取数据:
812¤SuperIT¤2015-12-06 18:00¤25
614¤MediaHuset¤2016-01-14 16:15¤67
如果我将“¤”替换为“;”,我的脚本就可以运行,但它不适用于“¤”、“¤
”、“¤
”
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('¤');
document.getElementById('table').innerHTML +=
"<h4>" + parts[0] + "</h4>" +
parts[1] + "<br/>" +
"Antall plasser: " + parts[2] + "<br/>";
}
}
}
HTML 看起来像这样:
<!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>
¤
可能只是一个不可打印的字符。使用 od -c
找出它到底是什么,然后拆分。
$ echo 'abc' | od -c
0000000 a b c \n
0000004
按“¤”def 拆分。有效,请参见此处:https://jsfiddle.net/s7t0c5jg/ --- 所以问题出在您的代码中的其他地方,或者您的浏览器很难处理“¤”,或者您将文件保存为 UTF8 或其他格式,“¤”可能是比 UTF8 更高的字符?
看了上面的评论我觉得Axel说的是对的 "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 来源:
<!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>