js:方法2. 字符串

时间:2023-01-04 17:07:02

String.charAt()/String.charCodeAt()

string.charAt(n); n:The index of the character that should be returned from string.

  var a = 'abc';
  a.charAt(2);   //=> 'c'
  a.charCodeAt(2);  //=>99

String.concat()

string.concat(value, ...)

  var a = 'abc';
  a.concat('def'); //=> 'abcdef'

String.fromCharCode()

String.fromCharCode(c1, c2, ...); c1, c2, ... Zero or more integers that specify the Unicode encodings of the characters in the string to be created.

// Create the string "hello"
var s = String.fromCharCode(104, 101, 108, 108, 111);

String.indexOf() /String.lastIndexOf()

string.indexOf(substring) ; string.indexOf(substring, start)

 var a = 'abcabc';
  a.indexOf('b'); //=> 1
  a.indexOf('b', 1); //=> 1
  a.indexOf('b', 2); //=> 4

String.match()

string.match(regexp)

"1 plus 2 equals 3".match(/\d+/g) // Returns ["1", "2", "3"]

var url = /(\w+):\/\/([\w.]+)\/(\S*)/;
var text = "Visit my home page at http://www.isp.com/~david";
var result = text.match(url);
if (result != null) {
 var fullurl = result[0]; // Contains "http://www.isp.com/~david"
 var protocol = result[1]; // Contains "http"
 var host = result[2]; // Contains "www.isp.com"
 var path = result[3]; // Contains "~david"
}

String.replace()

string.replace(regexp, replacement)

To ensure that the capitalization of the word “JavaScript” is correct:
text.replace(/javascript/i, "JavaScript");

To convert a single name from “Doe, John” format to “John Doe” format:
name.replace(/(\w+)\s*,\s*(\w+)/, "$2 $1");

To replace all double quotes with double back and forward single quotes:
text.replace(/"([^"]*)"/g, "''$1''");

To capitalize the first letter of all words in a string:
text.replace(/\b\w+\b/g, function(word) {
  return word.substring(0,1).toUpperCase() +
  word.substring(1);
});

String.search()

**string.search(regexp)###

var s = "JavaScript is fun";
s.search(/script/i) // Returns 4
s.search(/a(.)a/) // Returns 1

String.slice()

string.slice(start, end)

var s = "abcdefg";
s.slice(0,4) // Returns "abcd"
s.slice(2,4) // Returns "cd"
s.slice(4) // Returns "efg"
s.slice(3,-1) // Returns "def"
s.slice(3,-2) // Returns "de"
s.slice(-3,-1) // Should return "ef"; returns "abcdef" in IE 4

String.split()

string.split(delimiter, limit)

"1:2:3:4:5".split(":"); // Returns ["1","2","3","4","5"]
"|a|b|c|".split("|"); // Returns ["", "a", "b", "c", ""]

var words = sentence.split(/\s+/);

"hello".split(""); // Returns ["h","e","l","l","o"]
"hello".split("", 3); // Returns ["h","e","l"]

var text = "hello <b>world</b>";
text.split(/(<[^>]*>)/); // Returns ["hello ","<b>","world","</b>",""]

String.substr()

string.substr(start, length)

var s = "abcdefg";
s.substr(2,2); // Returns "cd"
s.substr(3); // Returns "defg"
s.substr(-3,2); // Should return "ef"; returns "ab" in IE 4

String.substring()

string.substring(from, to)

  var a = 'abcabc';
  a.substring(1,3); //=> bc

String.toLowerCase()/String.toLocaleLowerCase()/String.toUpperCase()/String.toLocaleUpperCase()

String.trim()