loading...

JavaScript Basic String Functions

k_penguin_sato profile image K-Sato ・2 min read

Here are some of the most fundamental JS string functions.

Assume the code below is declared at the top level in the following examples.

var string = "string"

Changing the case

toLowerCase()

Converts a string to lowercase letters.

let s6 = "STRING".toLowerCase();
console.log(s6); //=> "string"

toUpperCase()

Converts a string to uppercase letters.

let s7 = string.toUpperCase();
console.log(s7); //=> "STRING"

Searching for a substring

indexOf(subst, pos)

It looks for the substr in a string, starting from the given position pos.

console.log(string.indexOf('i')) //=> 3

let string2 = 'string string'

console.log(string2.indexOf('string', 2)) //=> 7

includes()

Checks whether a string contains the specified string/characters.

console.log(string.includes("s")); //=> true

match()

Searches a string for a match against a regular expression, and returns the matches.

const regex = /\w/g;
let s4 = string.match(regex);
console.log(s4); //=> ["s", "t", "r", "i", "n", "g"]

Getting a substring

slice()

Extracts a part of a string and returns a new string.

let s2 = string.slice(0, 3); ///=> str
console.log(s2);

substring()

Extracts a part of a string and returns a new string.

let s2 = string.substring(0, 3); ///=> str
console.log(s2);

Transforming a string

split()

Splits a string into an array of substrings.

let s1 = string.split("");
console.log(s1); //=> ["s", "t", "r", "i", "n", "g"]

concat()

Joins two or more strings, and returns a new joined strings.

let s3 = string.concat("string");
console.log(s3); //=> stringstring

replace()

Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

let s5 = string.replace(/s/, "S");
console.log(s5); //=> String

trim()

Removes whitespace from both ends of a string.

let s8 = " strn g   ".trim();
console.log(s8); //=> strn g

let s9 = "  string  ".trimLeft();
console.log(s9); //=> "string  "

let s10 = "  string    ".trimRight();
console.log(s10); //=> "  string"

References

Posted on by:

k_penguin_sato profile

K-Sato

@k_penguin_sato

I am a software-engineer based somewhere on earth.

Discussion

markdown guide
 

charAt(x) Returns the character at the “x” position within the string.charCodeAt(x) Returns the Unicode value of the character at position “x” within the string. concat(v1, v2,…). fromCharCode(c1, c2,…). Thanks for sharing this is such a great post that you are providing and you give it away for free. I love seeing blog that understand the value of providing quality information for free. I would like to suggest you that please keep on sharing such type of information with us. I really found it too much informative. It is what I was searching from many days. dissertation help service . Students assignment help.

 

Usually, I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up.

Visit: العاب كرتون نتورك بالعربية

 

As a programmer from essay writers in cheap with the help of string functions you can control much of behavior of website from fonts to even text or even background colors Good practicing example but it would be much better to demonstrate these functions in some handy real world project.

 

Strings are an important part of any programming language, and JavaScript has many powerful built-in functions that make it easy for developers to use strings. I am also a developer and associate with Masters Dissertation Help UK.