javascript JavaScript字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript字符串相关的知识,希望对你有一定的参考价值。

var me = "I am Batman";

var len = me.length; // Length of the string
// Output : 11 

me.indexOf('Bat'); // First occurance on text,
// Output : 5        -1 if not found

me.lastIndexOf("a"); // Last occurance of text
// Output : 9

me.slice(5,11); // Extract a section of the text
// Output : Batman

me.replace("Batman", "Robin"); // Find and replace 
// Output : I am Robin            specifed text

me.toUpperCase(); // Convert to upper case
// Output : I AM BATMAN

me.toLowerCase(); // Convert to lower case
// Output : i am batman

// Join two or more strings into one
me.concat(", I like ","apples"); 
// Output : I am Batman, I like apples

me.charAt(5); // Character at the index
// Output : B

me.charCodeAt(5); // Unicode of character
// Output : 66       at position

me.search('am'); // Search for string and 
// Output : 2       return its position

me[3];   // unsafe, abc[2] = "C" doesn't work

me.split(','); // Splits into an array of strings by commas
// Output : ["I am Batman"]

me.split(''); // Split by character
// Output : [ "I", " ", "a", "m", " ", "B", "a", "t", … ]

me.substr(5);  // extracts a substring depending 
// Output : Batman  on a specified number of characters

me.substring(5);  // Similar, can’t accept negative indices
// Output : Batman   

var value = 12345
value.toString(); // Convert the number to string

var letsBreakThis ="You can't \n break me";
// Output :"You can't 
//          break me"

以上是关于javascript JavaScript字符串的主要内容,如果未能解决你的问题,请参考以下文章

javascript 从数组中查找字符串的位置?

javascript JavaScript字符串

javascript JavaScript中的字符串

javascript 字符串,数字,方法JavaScript

JavaScript JavaScript:反向字符串[扩展本机]

JavaScript Javascript字符串替换(所有出现)