First I will explain the string basics and then will cover some important methods and properties. String is the group of characters like “Hello”, “565$”, “www.facebook.com”. The string is most usable type, Just to prove that if you have to display message you will use a string message.
Creation of String:
String objects are usually created using string literal. String literal may contain special characters (\n, \b, \r, \s) denoted by escape-sequences, a newline symbol and others. There are three ways of declaration
- Implicit String Declaration By assigning the "result" of a method which returns a value of String data type, This will create strings indirectly.
Listing 1: Quoted String Declaration
Var txt1 =’This is string tutorial’; Var txt2=”this is string tutorial”;
Listing 2: Constructor String Declaration
Var txt1=new String(“this is string tutorial”); Var txt2=new String(); //This is empty string
Listing 3: Implicit String Declaration
var txt1 = new Array( "this", "is", "String" , “tutorial”); var txt2 = txt1.join(" ");
String Function and Properties:
Sometimes it’s become too important to know the length of string. Advance web developer and script writer will often need to know string length.
Suppose you are creating a registration form and you have a text filed of address and you want text limit no longer than 30 characters ,Then you would have to check what is the current string length.
Following example will elaborate the method of the length
Listing 4: String length Property Example
“In the above example, “txt.length“ property calculate the total character including white spaces. Here the string length is 23.
If you have a long string like "This is tutorial on string" and you want to store each word separately, you can specify the space character " " and have the split function create a new chunk every time it sees a space.
Listing 5: Script of String Split Property
Figure 2: Above figure is output of script of split property.
In above example we have created a string with numbered words zero through six. The delimiter in this example will be the space character " ".
Sometimes it can be important to knowing that any particular substring or character exist in a string or not. This function takes a regular expression and then examines the string and if there is a match it will return the position in substring.
Listing 6: Script of String Search Property
Figure 3: Above figure is output of program of string matching
Following program will help you to understand the string replace property.
Listing 7: Script of String Replace Property
Figure 4: Above figure is output of String replace program.
This article captures the basic idea of implementing different kind of methods and property on string .There are many methods and property are available you can use like this. As usual if you have any queries please comment.