[英]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:我将从包含以下内容的 .dat 文件中获取数据:
812¤SuperIT¤2015-12-06 18:00¤25 812¤SuperIT¤2015-12-06 18:00¤25
614¤MediaHuset¤2016-01-14 16:15¤67 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('¤');
document.getElementById('table').innerHTML +=
"<h4>" + parts[0] + "</h4>" +
parts[1] + "<br/>" +
"Antall plasser: " + parts[2] + "<br/>";
}
}
}
The HTML looks like this: 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>
¤
is probably just a nonprintable character. ¤
可能只是一个不可打印的字符。 Use od -c
to find out what it actually is, and split on that.使用od -c
找出它实际上是什么,然后拆分它。
$ 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?有效,请参见此处: https : //jsfiddle.net/s7t0c5jg/ --- 所以问题出在您的代码中的其他地方,或者您的浏览器很难处理“¤”,或者您将文件保存为 UTF8 或其他格式“¤”可能是比 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"阅读上述评论后,我认为Axel说“通常 ¤ 仅表示不可打印的字符,并不意味着该字符实际上在您的数据中。使用十六进制编辑器打开数据文件并查看非常分隔符 ascii 值”
JSFiddle source: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.