I. Description
When using JavaScript, we always need to perform operations on strings, such as judging whether the string is empty, calculating the length of the string, replacing characters, searching for string patterns, slicing the string, and converting the string to other types.
This article describes how to manipulate strings through JavaScript.
II. The length of the string
2.1 Return string length
If you want to get the length of the string, you can use the length property of the string object.
Example:
var txt= "Hello FR";
alert("String length: "+txt.length); //return length
The result is: 8.
If you need to get the length of the string in a text widget, you can use the following code:
var strCtl = this.options.form.getWidgetByName("label1");
alert("String length: "+strCtl.getText().length); //return length
2.2 Determine whether the string is empty
If the string is empty, the length is 0. You can use the following method to determine whether the string is empty:
var txt= "";
if(txt.length==0||txt=='') alert("String is empty"); //is empty
else alert("String is not empty"); //not empty
If the string txt either satisfy txt.length==0 or txt=='', we can know the string is empty.
III. String Replacement
The string method replace(regexp, replacement) can search and replace string elements.
It will find substrings that match regexp, and then replace these substrings with replacement. If regexp has the global flag g, then the replace method will replace all matched substrings. Otherwise, it only replaces the first matched substring.
Example JavaScript codes are as follows:
var txt="Visit FineReport!";
var txt2=txt.replace(/Visit/, "Hello");
alert(txt2);
The result is: Hello FineReport!.
IV. Search for Strings
The string method search(regexp) is used to find a specified substring.
He will return the starting position of the first substring that matches the regexp (position starts from 0). If no matched substring is found, -1 will be returned.
The search method only finds the first occurrence of the matched substring, and cannot find all matched substrings.
For example:
var txt = "123456789abcdeabc";
var location=txt.search("abc");
alert(location);
The result is: 9.
V. String Slicing
We can use the string method substr(start,length) to extract part of the string.
start is the starting position from which to extract a substring whose length is length.
The sample JavaScript code is as follows:
var txt="Visit FR!";
var txt2=txt.substr(6,2); //Take two characters from the sixth
alert(txt2);
The result is: FR.
If start is negative, the method will start the slicing from the beginning.
VI. String Concatenation
Multiple strings can be concatenated through string method concat(str1,str2...).
for example:
var str1="hello";
var str2="FR";
var str3=str1.concat(str2);
alert(str3);
The result is: helloFR.
VII. String Type Conversion
7.1 Convert a string to a number
You can force the conversion directly.
If the goal is to convert to a floating point number, use string method parseFloat(str).
If the goal is to convert the string to an integer, string method parseInt(str, radix) should be used. The parameter radix represents the base of the number to be parsed and its value is between 2 and 36. If it is 2, the string is converted to a binary number. If this parameter is omitted or its value is 0, the string will be parsed based on 10, that is, converted to a decimal number. If the string starts with "0x" or "0X", it will be converted with base 16 as the hexadecimal number. If the parameter is less than 2 or greater than 36, parseInt will return NaN.
for example:
var str1=parseFloat("314e-2"); //return 3.14
alert(str1);
var str2=parseInt("17",8); //return 15
alert(str2);
The results are: 3.14 and 15.
7.2 Convert a string to an array
You can use string method split (separate) to split the string into an array.
The parameter separate is the delimiter for division.
for example:
var str1="I love FR";
var str2=str1.split(" ");
alert(str2);
str2 will be stored as a string array, and the value of each element is: "I", "love" and "FR".
7.3 Unified conversion of string letters to uppercase
You can use string method toUpperCase() to convert all letters in the string to uppercase.
var data="FineReport 10.0";
var newdata=data.toUpperCase(); // Converted to FINEREPORT 10.0
alert(newdata);
Note: The method toUpperCase()has no effect on non-alphabetic characters.
7.4 Unified conversion of string letters to lowercase
You can use string method toLowerCase() to convert all letters in the string to lowercase.
for example:
var data="FineReport 10.0";
var newdata=data.toLowerCase(); // Converted to finereport 10.0
alert(newdata);