How to Check a Substring in JavaScript

How to Check a Substring in JavaScript thumbnail
JavaScript string functions are particularly useful when handling user input.

Substring functions in JavaScript allow you to find out about the content of text strings in your Web page scripts. JavaScript string functions are particularly useful when processing user input. Using the substring functions, you can isolate parts of a string, using particular sections as you require them. To acquire a substring in JavaScript, you need to decide what part of the string you want. Once you have the substring, you can carry out further processing on it as you require, including checking its content.

Instructions

    • 1

      Prepare a string variable in your JavaScript code. To carry out the substring functions, you need an original string to start with. Use the following sample code to create a string variable:

      var fullString = "Here is a string with some text in it.";

      You can include any characters you like within your string variable, including punctuation symbols and numbers.

    • 2

      Carry out the "substring" function on your string. The "substring" function takes a string together with the start and end positions for the substring required, passing these positions as parameters. Use the following syntax to acquire a substring from your original string:

      var partString = fullString.substring(5, 9);//results in 'is a'

      The second parameter indicates the position after the last character to be included in the substring. You can alternatively acquire the substring starting at a set position and running to the end of the string by using only one parameter:

      var partString = fullString.substring(32);//results in 'in it'

    • 3

      Carry out the "substr" function on your string. The "substr" function is an alternative method, taking the start position and length of the desired substring. You can use the function as follows:

      var partString = fullString.substr(8, 13);//results in 'a string with'

      The second parameter indicates how many characters to include in the substring, starting at the position indicated by the first parameter. The "substr" function can also be used with one parameter:

      var partString = fullString.substr(35);//results in 'it'

    • 4

      Check your substring variable. Depending on what you plan on using your substring variable for, you may want to carry out further processing to check its value. To check the length, use the following syntax:

      var partLength = partString.length;

      This records the length of your new string as an integer value. If you simply want to check that the substring variable has a value, you can check whether the length is greater than zero:

      if(partLength>0) {

      //carry out further processing

      }

    • 5

      Save your file and test it. You can output information using the "alert" function while you are developing your scripts. The following writes out the length and content of the substring variable together with some informational text, presented within an alert dialog in the browser:

      alert("Substring: "+partString+" - Length: "+partLength);

      Open your page in a Web browser to test the JavaScript code functions.

Tips & Warnings

  • The string class in JavaScript has lots of other functions and properties you may find useful.

  • If your string functions are being carried out on user input, check first that the full string is not empty or your code will fail.

Related Searches:

References

Resources

  • Photo Credit Thinkstock Images/Comstock/Getty Images

Comments

You May Also Like

Related Ads

Featured