• Sunday , 24 September 2017

Strings in JavaScript

Code Canyon



Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/11/strings-in-javascript.html

In this video we will discuss different functions that are available to manipulate string in JavaScript.

A string is any text inside quotes. You can use either single or double quotes.
var string1 = “string in double quotes”
var string2 = ‘string in single quotes’

Concatenating strings : There are 2 options to concatenate strings in JavaScript. You could either use + operator or concat() method

Example : Concatenating strings using + operator
var string1 = “Hello”
var string2 = “JavaScript”
var result = string1 + ” ” + string2;
alert(result);

Output : Hello JavaScript

Example : Concatenating strings using concat() method
var string1 = “Hello”
var string2 = “JavaScript”
var result = string1.concat(” “, string2);
alert(result);

Output : Hello JavaScript

If you want single quotes inside a string, there are 2 options
Option 1 : Place your entire string in double quotes, and use single quotes inside the string wherever you need them.

Example :
var myString = “Welcome to ‘JavaScript’ Training”;
alert(myString);

Output : Welcome to ‘JavaScript’ Training

Option 2 : If you prefer to place your entire string in single quotes, then use escape sequence character with a single quote inside the string.

Example :
var myString = ‘Welcome to ‘JavaScript’ Training’;
alert(myString);

Output : Welcome to ‘JavaScript’ Training

Please Note : You can use the above 2 ways if you need double quotes inside a string

Converting a string to uppercase : Use toUpperCase() method

Example :
var upperCaseString = “JavaScript”;
alert(upperCaseString.toUpperCase());

Output : JAVASCRIPT

Converting a string to lowercase : Use toLowerCase() method

Example :
var lowerCaseString = “JavaScript”;
alert(lowerCaseString.toLowerCase());

Output : javascript

Length of string javascript : Use length property

Example : alert(“JavaScript”.length);

Output : 10

Example :
var myString = “Hello JavaScript”;
alert(myString.length);

Output : 16

Remove whitespace from both ends of a string : Use trim() method

Example :
var string1 = ” AB “;
var string2 = ” CD “;
var result = string1.trim() + string2.trim();
alert(result);

Output : ABCD

Replacing strings in javascript : Use replace() method. This method searches a given string for a specified value or a regular expression, replaces the specified values with the replacement values and returns a new string. This method does not change the original string.

Example : Replaces JavaScript with World
var myString = “Hello JavaScript”;
var result = myString.replace(“JavaScript”, “World”);
alert(result);

Output : Hello World

Example : Perform a case-sensitive global replacement. In this example, we are using a regular expression between the 2 forward slashes(//). The letter g after the forward slash specifies a global replacement. The match here is case sensitive. This means Blue(with capital B) is not replaced with green.

var myString = “A Blue bottle with a blue liquid is on a blue table”;
var result = myString.replace(/blue/g, “green”);
alert(result);

Output : A Blue bottle with a green liquid is on a green table

Example : Perform a case-insensitive global replacement. The letters gi after the forward slash indicates to do global case-insensitive replacement. Notice that the word Blue(with capital B) is also replaced with green.

Original source

3d Ocean

Related Posts

16 Comments

  1. kingsley chibuzor
    June 27, 2017 at 05:36

    Thank you sir

  2. mahmoud elkout
    June 27, 2017 at 05:36

    excellent

  3. Neddih Lous
    June 27, 2017 at 05:36

    pure awesome;

  4. Vignesh Ravi
    June 27, 2017 at 05:36

    You do all explanations very good and with proper speed. Thanks man, you helped me a lot

  5. Hakan Koluaçık
    June 27, 2017 at 05:36

    You do all explanations very good and with proper speed. Thanks man, you helped me a lot

  6. Amit Sharma
    June 27, 2017 at 05:36

    Nice way to explain

  7. Hiếu Trương
    June 27, 2017 at 05:36

    you are the best teacher in the world ,thank a lot from VietNam

  8. RAQIBUL ALAM RASHED
    June 27, 2017 at 05:36

    Thanks abillions.

  9. LuqMaN AFridi
    June 27, 2017 at 05:36

    excellent mehtod !!!
    I m very thankful of U Sir >>>

  10. Hashmat Rai
    June 27, 2017 at 05:36

    amazing very clear explanation..thank you sir 🙂

  11. Sotra
    June 27, 2017 at 05:36

    YOU'RE THE BEST+EST ! Thanks!

  12. norvasc28
    June 27, 2017 at 05:36

    clear easy explanation. to-the-point. full of practical examples & applications of javascript.. Thank u very much Venkat

  13. MrRicardo141
    June 27, 2017 at 05:36

    This guy covers everything. FANSTASTIC!

  14. Alejandro Rincon
    June 27, 2017 at 05:36

    Great tutorial thank you very much

  15. Sudeep Makwana
    June 27, 2017 at 05:36

    Thanks for video +kudvenkat  

  16. kaminari66
    June 27, 2017 at 05:36

    Great video!  You may want to note that string.trim() is not supported in older IE versions such as IE8.

Leave A Comment

You must be logged in to post a comment.