JavaScript string methods

      No Comments on JavaScript string methods

JavaScript string Methods and Properties :Primitive values, like “Scanfcode”, cannot have properties or methods (because they are not objects).

In JavaScript methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties

JavaScript string methods:

MethodDescription

charAt()

Returns the character at the specified index.

charCodeAt()

Returns a number indicating the Unicode value of the character at the given index.

concat()

Combines the text of two strings and returns a new string.

indexOf()

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

lastIndexOf()

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

localeCompare()

Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

match()

Used to match a regular expression against a string.

replace()

Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

search()

Executes the search for a match between a regular expression and a specified string.

slice()

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

split()

Splits a String object into an array of strings by separating the string into substrings.

substr()

Returns the characters in a string beginning at the specified location through the specified number of characters.

substring()

Returns the characters in a string between two indexes into the string.

toLocaleLowerCase()

The characters within a string are converted to lower case while respecting the current locale.

toLocaleUpperCase()

The characters within a string are converted to upper case while respecting the current locale.

toLowerCase()

Returns the calling string value converted to lower case.

toString()

Returns a string representing the specified object.

toUpperCase()

Returns the calling string value converted to uppercase.

valueOf()

Returns the primitive value of the specified object.

JavaScript string methods for finding strings

String indexOf() method

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

<script>
function myfunction()
{
var string1=document.getElementById("sample").innerHTML;
var p=string1.indexOf("World");
document.getElementById("answer1").innerHTML=p;
}
</script>
<p id="sample">Hello World,Scanfcode</p>
<p id="answer1"></p>
<button onclick="myfunction()">Try</button>

Hello World,Scanfcode

String lastIndexOf()

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

<script>
function myfunction_lastindexof()
{
var string1=document.getElementById("sample1").innerHTML;
var p=string1.indexOf("string");
document.getElementById("answer2").innerHTML=p;
}
</script>
<p id="sample1">lastIndexOf() string function returns last occurrence of a specified text string.</p>
<p id="answer2"></p>
<button onclick="myfunction_lastindexof()">Try</button>

lastIndexOf() string function returns last occurrence of a specified text string.

NOTE Both indexOf() and lastIndexOf() functions return -1 if string is not found.

String search()

search() method searches for a string and returns the location of match. indexOf() and search() functions are same in most cases, that they take similar arguments and return same value but search() method can take more powerful search values.

<script>
function mysearch()
{
var string1=document.getElementById("sample2").innerHTML;
var p=string1.search("code");
document.getElementById("answer3").innerHTML=p;
}
</script>
<p id="sample2">Scanfcode, code wants to be simple</p>
<p id="answer3"></p>
<button onclick="mysearch()">Try</button>

Scanfcode, code wants to be simple

Extracting string parts

slice() method

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

<p id="answer4"></p>
<script>
var string1="Red, Orange, Blue, White";
document.getElementById("answer4").innerHTML=string1.slice(8,13);
</script>

ange,

string.slice(-12,-6); will start the counting from end and this will slice out positions -12 to -6.
string1.slice(7); will slice out rest of the string starting from position 7.

substring() method

Returns the characters in a string between two indexes into the string.

<p id="answer5"></p>
<script>
var string1="Red, Orange, Blue, White";
document.getElementById("answer5").innerHTML=string1.substring(6,13);
</script>

range,

substr() method

substr() method is similar to slice() method except that the second parameter here is the length.

<p id="demo"></p>
<script>
var string1="Red, Orange, Blue, White";
document.getElementById("demo").innerHTML=string1.substr(6,13);
</script>

replace() method

This method replaces a specified value with other

<script>
function myreplace()
{
  var str1=document.getElementById('demo1').innerHTML;
  var text=str1.replace("Apple" , "Scanfcode");
  document.getElementById('demo1').innerHTML=text;
}
</script>
<p id='demo1'>Please visit Apple</p>
<button onclick="myreplace()">Try</button>

Please visit Apple

Converting to upper and lower case

toLowerCase() converts the string to lower case characters and toUpperCase() converts the string characters to uppercase.

<script>
function lower()
{
  var str2=document.getElementById('demo2').innerHTML;
  document.getElementById('demo2').innerHTML=str2.toLowerCase();
}
function upper()
{
  var str3=document.getElementById('demo2').innerHTML;
  document.getElementById('demo2').innerHTML=str3.toUpperCase();
}
</script>
<p id='demo2'>Welcome to Scanfcode.com</p>
<button onclick="lower()">convert to lower case</button>
<button onclick="upper()">convert to upper case</button>

Welcome to Scanfcode.com

concat() method

concat() method joins two strings

<p id="demo3"></p>
<script>
var string_a="Hello";
var string_b="World";
document.getElementById("demo3").innerHTML=string_a.concat(" ",string_b);
</script>

JavaScript string methods for extracting string characters

The charAt() method

charAt() method returns character at a specified index in a string.

<p id="test"></p>
<script>
 var string_c="Scanfcode";
 document.getElementById('test').innerHTML=string_c.charAt(0);
</script>

charCodeAt() method

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

<p id="test1"></p>
<script>
 var string_d="Scanfcode";
 document.getElementById('test1').innerHTML=string_d.charCodeAt(0);
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *