js之字符串

  • 字符串

1.內置對象
在JavaScript中已經定義好的對象

比較常用的 JavaScript 內置對象主要有以下幾種:
(1)字符串對象 String;
(2)日期對象 Date;
(3)數組對象 Array;
(4)數值對象 Math 和 Number;

2.字符串屬性 : length
可以用來獲取字符串的長度

  • 處理字符串的方法(11種)
(1)match():查詢字符串中字符串是否存在,存在就輸出
			var str1='java c++ c# python html css';
			alert(str1.length);  獲取字符串的長度
			document.write(str1.match('python'));	
			返回null值: 表示沒有查詢到love
			document.write(str1.match('Python')); 

			(2)search():檢索字符串中字符所在的索引,返回索引值
			document.write(str1.search('Sc'));
			
			(3)indexOf():返回元素在在字符串中第一次出現的索引
			document.write(str1.indexOf('a'));
			
			(4)replace():在字符串中用一些字符替換另外一些字符
			document.write(str1.replace('JavaScript','InLett'));

			(5)charAt():獲取字符串中制定字符
			document.write(str1.charAt(3));
			
			(6)大小寫轉換:tolowerCase()和toUpperCase()
			document.write(str1.toLowerCase());
			document.write(str1.toUpperCase());

			(7)concat():鏈接字符串
			document.write(str1.concat(',HTML',',CSS'));
			
			(8)比較字符串localeCompare(): 根據ASCII表的排列進行比較
			var str2='java c++ c# python html css';
			document.write(str2.localeCompare('avaScript'));
			
			(9)split():分割字符串(分割符是以字符串中的字符進行分割)  返回一個數組
			var arr=new Array();
			arr=str2.split(',');
			document.write(arr);
			document.write(arr[0]);
			document.write(arr[1]);
			
			(10)subString(start,end):截取字符串
			var str3='java c++ c# python html css';
			不包含結束的索引對應的值 
			document.write(str3.substring(0,2));
			document.write(str3.substring(3,38));
			
			document.write(str3.substring(4,2));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章