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在 一個Data Encryption Service器上,其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個元素就可得出網域名稱. |