logo

NJP

Useful String Methods

Import · Oct 07, 2020 · article

Length

The length property returns the length of a string.

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

IndexOf

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string

var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");

The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");

Both indexOf(), and lastIndexOf() return -1 if the text is not found.

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("John");

Search

The search() method searches a string for a specified value and returns the position of the match:

var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");

The slice() Method

slice() extracts a part of a string and returns the extracted part in a new string.

The method takes 2 parameters: the start position, and the end position (end not included).

var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13);

If a parameter is negative, the position is counted from the end of the string.

var str = "Apple, Banana, Kiwi";
var res = str.slice(-12, -6);

If you omit the second parameter, the method will slice out the rest of the string:

var res = str.slice(7);

Substring

substring() is similar to slice().

The difference is that substring() cannot accept negative indexes.

var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);

If you omit the second parameter, substring() will slice out the rest of the string.

Substr

substr() is similar to slice().

The difference is that the second parameter specifies the length of the extracted part.

var str = "Apple, Banana, Kiwi";
var res = str.substr(7, 6);

Replace

The replace() method replaces a specified value with another value in a string:

str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "Apple");

By default, the replace() method replaces only the first match:

str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "Apple");

Converting to Upper and Lower Case

A string is converted to upper case with toUpperCase():

var text1 = "Hello World!";       // String
var text2 = text1.toUpperCase();  // text2 is text1 converted to upper

A string is converted to lower case with toLowerCase():

var text1 = "Hello World!";       // String
var text2 = text1.toLowerCase();  // text2 is text1 converted to lower

Concat

concat() joins two or more strings:

var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);

The concat() method can be used instead of the plus operator. These two lines do the same:

var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ", "World!");

Trim

The trim() method removes whitespace from both sides of a string:

var str = "       Hello World!        ";
alert(str.trim());//"Hello World!"

CharAt

The charAt() method returns the character at a specified index (position) in a string:

var str = "HELLO WORLD";
str.charAt(0);// returns H

CharCodeAt

The charCodeAt() method returns the unicode of the character at a specified index in a string:

The method returns a UTF-16 code (an integer between 0 and 65535).

var str = "HELLO WORLD";
str.charCodeAt(0);         // returns 72

Property Access

ECMAScript 5 (2009) allows property access [ ] on strings:

var str = "HELLO WORLD";
str[0]; // returns H

Converting a String to an Array

A string can be converted to an array with the split() method:

var txt = "a,b,c,d,e";   // String
txt.split(",");          // Split on commas
txt.split(" ");          // Split on spaces
txt.split("|");          // Split on pipe
var txt = "Hello";       // String
txt.split("");           // Split in characters

My Articles:

1. Useful Number methods in JavaScript

2. Service-Now Client Script APIs

3. VS Code setup for servicenow

I hope this article helpful. Please mark it as helpful and bookmark if you like it.

“Try to learn something about everything and everything about something.” – Thomas H. Huxley

Thanks,

Phanindra.

View original source

https://www.servicenow.com/community/developer-articles/useful-string-methods/ta-p/2324172