-
Notifications
You must be signed in to change notification settings - Fork 0
String
KS LIM edited this page Mar 19, 2021
·
7 revisions
- split()
- slice()
- substring()
- match()
- replace() - can be used to remove white space
1.1 split() Method -> to split a string into an array of substrings, and returns the new array.
1.2 slice() Method -> extracts parts of a string and returns the extracted parts in a new string. Use the start and end parameters to specify the part of the string you want to extract. The first character has the position 0, the second has position 1, and so on.
1.3 substring() Method -> extracts the characters from a string, between two specified indices, and returns the new sub string. home
EXAMPLE 1:
var s = "abc-123";
var s1 = String(s).split("-");
var s2 = String(s1.slice(0,1).substring(0,1);
OUTPUT:
s1.slice(0,1) => abc
s2 => a
EXAMPLE 2:
var t = "20161205";
var s1 = t.substring(0,4);
var s2 = t.substring(4,6);
var s3 = t.substring(6,8);
OUTPUT:
s1 => 2016
s2 => 12
s3 => 05
-searches a string for a match against a regular expression, and returns the matches, as an Array object. home
var str = "あいお";
if(!str.match(/[^A-Z a-z 0-9 \s]+/)){
alert("hiragana or katakana was detected");
}
- searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
var str = "a bc d e";
str.replace(/\s+/g,''); // output => "abcde"