During report development, some requirements may not be implemented through existing functionality. Developers are required to develop two times, taking Finereport as an example. Be able to use Web script, API interface, etc. for in-depth development and control.
Consider the use of JS script development more. First of all, we first introduce how to use JS to manipulate strings. For example, how to infer whether a string is empty, the length of a string, or a replacement. Find, intercept, or convert a string to another type.
1. Length of the string
1.1 Return string length
Gets the string length. Ability to use the length property of a String object.
Like what:
var txt= "Hello FR"; txt.length; return length
It will return 8.
1.2 Null for string
The string is empty, that is, the string length is 0. To achieve an empty sentence. Can be used such as the following methods:
if (txt.length==0| | txt== ") return true; is empty elsereturn false; is not empty
Here, txt.length==0 or txt== ' just need to meet one to
2. Substitution of strings
The replace () method of the string Stringobject runs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement. Assuming that RegExp has global flag G, the Replace () method replaces all of the matched substrings. Otherwise, it simply replaces the first matching substring.
Here is an example of a string substitution:
var txt= "Visit fr!"; Txt.replace (/visit/, "Hello");
The result will return Hello fr!
3. Searching for strings
The search (RegExp) method is used to retrieve the substring specified in the string. He will return the starting position of the first substring in stringobject that matches the regexp.
Suppose not found. will return-1.
However, the search method cannot find the global. Finds only the first occurrence of a matching string.
var txt= "Visit fr!"; Txt.replace (/visit/, "Hello");
4. Interception of strings
We can use the substr (Start,length) method to extract some of the contents of a string.
In which start is the start, and the length of the new string is extracted.
Give me a sample.
var txt= "Visit fr!"; Txt.substr (6,2); Start with the sixth and go to two.
The above results will return FR
The string subscript starts at 0, assuming start is negative, and the default is extracted from the beginning.
5. Connection of strings
The ability to concatenate multiple strings by using the concat (Str1,str2 ...) method of the string
Example
var str1= "Hello"; var str2= "FR"; Str1.concat (STR2);
The result will return a Hello FR
6. String Type conversions
6.1 Converting a string to a numeric value
Can be used to cast directly.
parsefloat (str) if it is converted to a floating-point number
parseint () Assuming the conversion to an integer type
6.2 Converting a string to an array
You can use the split (separate) method of the string to cut a string into an array.
Separate to filter strings.
Example
var str1= "I love FR"; var str2=str1.split ("");
STR2 will be stored as an array of strings with values of "I", "Love", "FR", respectively.
Web Reporting Tools Finereport Two-time development JS string