Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks

javascript 字符串处理

文章来源:http://lmgq.vip.sina.com/tech/jsadvancedlesson/c2p1.htm

一、声明字符串:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);

var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " + bold_monkey);

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

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

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

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

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

indexOf更实用之处:
var the_email = prompt("What’s your email address?", "");
var the_at_is_at = the_email.indexOf("@");
if (the_at_is_at == -1)
{
    alert("You loser, email addresses must 
    have @ signs in them.");
}

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

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

3、charAt 
chatAt方法用于发现一个字符串中某个特定位置的字符。
这里是一个例子:
var the_word = "monkey";
var the_first_letter = the_word.charAt(0);
var the_second_letter = the_word.charAt(1);
var the_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.length是6。不要忘记在一个字符串中第1个字符的

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

the_word.length-1。>>

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

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

var the_substring = the_string.substring(from, to);

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

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

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

var the_string = "monkey";
var clergy = the_string.substring(0,4);
var tool = the_string.substring(3,6);

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

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

var the_url = prompt("What’s the URL?","");
var lead_slashes = the_url.indexOf("//");
var domain_start = lead_slashes + 2;
var without_resource = the_url.substring(domain_start, the_url.length);
var next_slash = without_resource.indexOf("/");
var domain = without_resource.substring(0, next_slash);

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

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

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

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

置从5开始.

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

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

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

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

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

var domain_start = lead_slashes + 2; 

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

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

var without_resource = the_url.substring(domain_start, the_string.length); 

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

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

var next_slash = without_resource.indexOf("/"); 

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

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

var domain = without_resource.substring(0, next_slash); 

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

等同于"www.webmonkey.com"。

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

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

var my_friends ="trixie,moxie,sven,guido,hermes";

var friend_array =my_friends.split(",");

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

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

立一个数组,所以你无需使用new Array().

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

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

var the_url = prompt("What’s the URL?","");
var first_split = the_url.split("//");
var without_resource = first_split[1];
var second_split = without_resource.split("/");
var domain = second_split[0];

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

var the_url = prompt("What’s the URL?",""); 

提示用户输入一个URL,假设用户输入
"http://www.webmonkey.com/javascript/index.html"; .
var first_split = the_url.split("//"); 
将用户输入的字符串分割成两块:first_split[0]是"http:",first_split[1]是

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

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

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

var domain = second_split[0]; 

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

posted on 2010-04-15 23:47 seal 阅读(283) 评论(0)  编辑  收藏 所属分类: Javascript

只有注册用户登录后才能发表评论。


网站导航: