Công Nghệ

JavaScript tutorial 104 – JavaScript String Methods | String Functions




String object properties and methods:

var name = “I am javascript language”;
document.write(name);

Note: when we use member access operator (dot) on a primitive string variable it is implicitly converted to a string object.

Properties:
length:
– returns the total number of characters present in a given string
Ex:
document.write(name.length); // 24

Methods:

charAt(index:Number):String
– returns a new string containing a character at the specified index
Ex:
document.write(name.charAt(2)); // a

charCodeAt(index:Number):Number
– returns the numeric ASCII value of the character at the specified index
Ex:
document.write(name.charCodeAt(2)); //97

indexOf(value:String):Number
– searches the string from left to right and returns the index of the first occurrence of the given value
Ex:
document.write(name.indexOf(“a”)); // 2

lastIndexOf(value:String):Number
– searches the string from left to right and returns the index of the last occurrence of the given value
Ex:
document.write(name.lastIndexOf(“a”)); // 21

toLowerCase():String
– returns a new string, with all uppercase characters converted to lowercase
Ex:
document.write(name.toLowerCase()); // i am javascript language

toUpperCase():String
– returns a new string, with all lowercase characters converted to uppercase
Ex:
document.write(name.toUpperCase()); // I AM JAVASCRIPT LANGUAGE

substr(startIndex:Number, len:Number):String
– returns a substring consisting of the characters that start at the specified startIndex and with a length specified by len
Ex:
document.write(name.substr(2,2)); // am

substring(startIndex:Number, endIndex:Number):String
– returns a string consisting of the characters specified by startIndex and all characters up to endIndex -1
Ex:
document.write(name.substring(2,4)); // am

slice(startIndex:Number, endIndex:Number):String
– returns a string that includes the startIndex character and all characters up to endIndex -1
Ex:
document.write(name.slice(2,4)); // am

startsWith(value:String):Boolean
– returns true if this string begins with the given string value else returns false
Ex:
document.write(name.startsWith(“I”)); // true

endsWith(value:String):Boolean
– returns true if this string ends with the given string value else returns false
Ex:
document.write(name.endsWith(“language”)); // true

concat(str1:String, str2:String,… ):String
– returns a new string by combining the given strings
Ex:
document.write( “I”.concat(” am”,” JavaScript”) );
document.write(“I” + ” am” + ” JavaScipt”);

split(seperator:String):Array
– splits a String object into an array of substrings by dividing it wherever the specified seperator occurs
Ex:
var words = name.split(” “);
document.write(words[0]); // I
document.write(words[1]); // am
document.write(words[2]); // javascript
document.write(words[3]); // language

valueOf():String
– returns the primitive value of a String object
Ex:
var number = new String(“244”);
document.write(typeof number); // object
document.write(number.valueOf());
document.write(typeof number.valueOf()); // string

=========================================

Follow the link for previous video:
JavaScript tutorial 103 – Creating Strings in Javascript

=========================================

Nguồn: https://vinhtrinh.com.vn

Xem thêm bài viết khác: https://vinhtrinh.com.vn/cong-nghe/

Công Nghệ
Kled Montage – Don't Gank Me
Công Nghệ
Sony G Master รีวิวเรียงตัว ฉบับดูจบแล้วเลือกถูกเลย!! | KP | KhuiPhai
Công Nghệ
[Playlist] Nhạc phim Trần Tình Lệnh – The Untamed OST
There are currently no comments.