简体   繁体   English

Javascript子字符串未返回正确的字符串

[英]Javascript substring doesn't return the correct string

i was manipulating the text in this document http://www.dlib.org/dlib/november14/brook/11brook.html , while i noticed something strange. 我正在处理此文档中的文本http://www.dlib.org/dlib/november14/brook/11brook.html ,而我发现了一些奇怪的地方。 I was looking at the beginning of the article, in particular here: 我正在看文章的开头,尤其是在这里:

Michelle Brook
The Content Mine
michelle@contentmine.org
...

(I am using jquery xpath)

var string=$("document").xpath("form[1]/table[3]/tr/td/table[5]/tr/td/table[1]/tr/td[2]/p[2]").html();
var new_string=string.substring(0,14);

I would expect that new_string was "Michelle Brook", but it was "Michelle Bro". 我希望new_string是“ Michelle Brook”,但它是“ Michelle Bro”。 Why that? 为什么? Is there a particular char that makes string.substring() fail? 是否存在使string.substring()失败的特定字符?

在尝试对字符串进行子字符串化之前,请使用以下内容从字符串中删除所有开头和结尾的空格:

var new_string=string.replace(/^\s*/, '').replace(/\s*$/, '').substring(0,14);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM