`
gift5168xf
  • 浏览: 28278 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Javascript对字符串的处理

阅读更多

一、声明字符串:
varnormal_monkey="Iamamonkey!<br>";
document.writeln("Normalmonkey"+normal_monkey);

varbold_monkey=normal_monkey.bold();
document.writeln("Boldmonkey"+bold_monkey);

这里的声明:varbold_monkey=normal_monkey.bold();
和下面对声明是等同的:
varbold_monkey="<b>"+normal_monkey+"</b>";

1 个版本的声明看起来要简明得多。这里用到了字符串对象中
bold 对象,其他的字符串对象还有indexOf,charAt,
substring,
以及split, 这些方法可以深入字符串的组成结构。
首先我们研究一下indexOf

2indexOf
indexOf
用于发现一系列的字符在一个字符串中等位置并告诉你子字符串的起始位置。如

果一个字符串中部包含该子字符串则indexOf 返回returns"-1."
例子:
varthe_word="monkey";
//
让我们从单词"monkey" 开始。
varlocation_of_m=the_word.indexOf("m");
//location_of_m(
字母m 的位置) 将为0 ,因为字母m 位于该字符串的起始位置。
varlocation_of_o=the_word.indexOf("o");
//location_of_o(
字母o 的位置) 将为1
varlocation_of_key=the_word.indexOf("key");
//location_of_key(key
的位置) 将为3 因为子字符串“key” 以字母k 开始,而k

在单词monkey 中的位置是3
varlocation_of_y=the_word.indexOf("y");
//location_of_y)
字母y 的位置)5
varcheeky=the_word.indexOf("q");
//cheeky
值是-1 ,因为在单词“monkey” 中没有字母q

indexOf 更实用之处:
varthe_email=prompt("What’syouremailaddress?","");
varthe_at_is_at=the_email.indexOf("@");
if(the_at_is_at==-1)
{
alert("Youloser,emailaddressesmust
have@signsinthem.");
}

这段代码询问用户的电子邮件地址,如果用户输入的电子邮件地址中不包含字符 则 提

示用户"@你输入的电子邮件地址无效,电子邮件的地址必须包含字符@ 。"

3charAt
chatAt
方法用于发现一个字符串中某个特定位置的字符。
这里是一个例子:
varthe_word="monkey";
varthe_first_letter=the_word.charAt(0);
varthe_second_letter=the_word.charAt(1);
varthe_last_letter=the_word.charAt(the_word.length-1);

the_first_letter(1 个字符)"m"
the_second_letter(
2 个字符)"o"
the_last_letter(
最后一个字符)"y"

注意利用字符串的length( 长度) 属性你可以发现在包含多少个字符。在本例中,

the_word"monkey" ,所以the_word.length6 。不要忘记在一个字符串中第1 个字符的

位置是0 ,所以最后一个字符的位置就是length-1 。所以在最后一行中用了

the_word.length-1>>

4 、子字符串(substring
子字符串(substring )和charAt 有些象,不同之处在于它能够从一个单词中抓取整个的

子字符串,而不只是字母,这里是其格式:

varthe_substring=the_string.substring(from,to);

"From" 指的是子字符串中第1个字母的位置,"to" 有点奇特,它是该子字符串中比最后

一个位置大1的位置.使用这种神奇的方法你可以标记子字符串的起始和结束位置,用

"to" 的位置减去"from" 的位置就会得出该子字符串的长度:

varthe_string="monkey";
varclergy=the_string.substring(0,4);
vartool=the_string.substring(3,6);

运行该段代码后变量clergy 的值为"monk"; 变量tool 的值为"key"

子字符串常和indexOf 一起使用,将字符串分成若干块.例如,
你可以从一个给定的URL 中抽取出其域名:

varthe_url=prompt("What’stheURL?","");
varlead_slashes=the_url.indexOf("//");
vardomain_start=lead_slashes+2;
varwithout_resource=the_url.substring(domain_start,the_url.length);
varnext_slash=without_resource.indexOf("/");
vardomain=without_resource.substring(0,next_slash);

这段代码的意思是:如果你输入
"http://www.webmonkey.com/javascript/index.html" ;
,则域名就是

"www.webmonkey.com" .如果这个方法对你来说有些麻烦,我将向你介绍如何使用split

方法简化其执行过程.但是首先我们作一些分析.

基本的技巧是将第1个斜杠和第2个斜杠之间的内容分离出来:
varthe_url=prompt("What’stheURL?","");
//
这行代码向用户询问一个URL .假设用户输入了
"http://www.webmonkey.com/javascript/index.html." ;
varlead_slashes=the_url.indexOf("//");
这行代码确定第一个双斜杠的位置.在本例中lead_slashes 的值是5,因为双斜杠的位

置从5开始.

你可能会想,通常的URL 都是以http:// 开始,所以双斜杠的位置肯定是在5开始,为什

么还要加入indexOf 这一段多余的代码呢?但是问题的关键在于你不知道用户在填入URL

时是否一定填入http: ,他们也许会不小心多键入了一个空格,也许他们所键入的URL

一个加密服务器上,其URL"https://www.whatever.com/" ; .在编程你必须预料到种种

可能发生的问题.所以我们必须用indexOf 方法确定双斜杠的确切的起始位置.

vardomain_start=lead_slashes+2;

这行代码用于计算该域名的第1个字母的起始位置.由于这里有一个双斜杠,所以域名

第1个字母的起始位置应该在双斜杠所在位置加2的位置.

varwithout_resource=the_url.substring(domain_start,the_string.length);

这段代码将域名起始位置往后的所有字符都提取出来.所以执行完这行代码后

without_resource"www.webmonkey.com/javascript/index.html."

varnext_slash=without_resource.indexOf("/");

这行代码计算出该字符串中下一个斜杠的位置,而从该字符串起始位置到这个斜杠之间

的内容就是域名.在本例中下一个斜杠的位置是17

vardomain=without_resource.substring(0,next_slash);

最后一步是提取出该字符串起始位置到下一个斜杠之间的所有内容.在本例中使得域名

等同于"www.webmonkey.com"

这样做确实很麻烦,利用split 方法则可以使该过程容易很多.>>

5 、分割方法(splittingmethod)
你可以使用split 方法用限位器来分割一系列的名称,然后将其
放在一个数组中.例如:

varmy_friends="trixie,moxie,sven,guido,hermes";

varfriend_array=my_friends.split(",");

for(loop=0;loop<friend_array.length;loop++)
{document.writeln(friend_array[loop]+"ismyfriend.<br>");}

这段代码将字符串my_friends 分割成包含5个元素的数组.JavaScript 可以为你自动建

立一个数组,所以你无需使用newArray()

将字符串分割成数组之后,我们使用了循环语句写出每一个名称.我们可以利用split

法简化前面所讲到的域名提取:

varthe_url=prompt("What’stheURL?","");
varfirst_split=the_url.split("//");
varwithout_resource=first_split[1];
varsecond_split=without_resource.split("/");
vardomain=second_split[0];

这段代码简化了很多而且也更容易理解.我们来分析一些这段代码:

varthe_url=prompt("What’stheURL?","");

提示用户输入一个URL ,假设用户输入
"http://www.webmonkey.com/javascript/index.html" ;

varfirst_split=the_url.split("//");
将用户输入的字符串分割成两块:first_split[0]"http:"first_split[1]

"www.webmonkey.com/javascript/index.html."
varwithout_resource=first_split[1];
//
提取出数组中的第2个元素,所以现在without_resource

"www.webmonkey.com/javascript/index.html."
varsecond_split=without_resource.split("/");
without_resource 分割成3块:www.webmonkey.com,javascript,index.html .现

在你可以看到split 的用途了吧?

vardomain=second_split[0];

现在我们提取出新数组中的第1个元素就可得出域名.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics