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("http://");
          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("http://"); 
          這行代碼確定第一個雙斜杠的位置.在本例中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方

          法簡化前面所講到的域名提?。?/p>

          var the_url = prompt("What’s the URL?","");
          var first_split = the_url.split("http://");
          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("http://"); 
          將用戶輸入的字符串分割成兩塊: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 閱讀(311) 評論(0)  編輯  收藏 所屬分類: Javascript
          主站蜘蛛池模板: 攀枝花市| 淮北市| 湛江市| 天祝| 寿阳县| 丹棱县| 资溪县| 浑源县| 怀宁县| 九台市| 辰溪县| 麻阳| 元朗区| 三河市| 博客| 银川市| 台山市| 黎川县| 扎鲁特旗| 睢宁县| 体育| 革吉县| 上高县| 湘西| 宜阳县| 浦东新区| 江城| 灌阳县| 邢台县| 邻水| 九龙城区| 郓城县| 贞丰县| 廊坊市| 利辛县| 广德县| 汪清县| 余江县| 平阴县| 射阳县| 富裕县|