In Javaweb development, string processing is often required, including the Java language and JS language, always easy to confuse, here is a simple comparison of the two languages for the string interception method.
First, look at Java
1 Public classStringdemo {2 Public Static voidMain (string[] args) {3String str = "Hello World";4 //string substring (int beginindex) returns a new string that is a substring of this string. 5System.out.println (Str.substring (3));//Lo World6 //string substring (int beginindex, int endIndex) returns a new string that is a substring of this string. 7System.out.println (Str.substring (3, 8));//Lo wo (including left side excluding right)8 }9}
Second, look at JavaScript
1, substr ()
Definition: The substr () method extracts a specified number of characters from the start subscript in a string.
Syntax: Stringobject.substr (start,length)
Parameters |
Description |
Start |
Necessary. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on. |
Length |
Optional. The number of characters in the substring. Must be numeric. If this argument is omitted, then the string from the beginning of the stringobject to the end is returned. |
Return value: A new string containing the lenght characters starting at start of Stringobject, including the character that the start refers to. If lenght is not specified, the returned string contains the character from start to end of Stringobject.
Tips and Comments:
(1) Note: the substr () parameter specifies the starting position and length of the substring, so it can be used instead of substring () and slice ().
(2) Important: ECMAscript did not standardize the method and therefore objected to its use.
(3) Important: In IE 4, the value of the parameter start is invalid. In this BUG, start specifies the position of the No. 0 character. In a later release, this bug has been fixed.
Example:
1 <script type= "Text/javascript" >2 var str = "Hello world"; 3 // 4 console.log (STR.SUBSTR (3)); // Lo World 5 Console.log (Str.substr (3,7)); // Lo worl 6 Console.log (Str.substr (3,8)); // Lo World 7 </script>
2, substring
Definition and Usage: the substring () method is used to extract the character of a string intermediary between two specified subscripts.
Syntax format: stringobject.substring (start,stop)
Parameters |
Description |
Start |
Necessary. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject. |
Stop |
Optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string. |
Return value: A new string value that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.
Description
(1) The substring returned by the substring () method includes the character at start, but does not include the character at end.
(2) If the argument start is equal to end, then the method returns an empty string (that is, a string of length 0). If start is larger than end, then the method swaps the two parameters before extracting the substring.
Hints and notes: Important: Unlike the slice () and substr () methods, substring () does not accept negative arguments.
Example:
1 <script type= "Text/javascript" >2 var str = "Hello world"; 3 // 4 console.log (str.substring (3)); // Lo World 5 Console.log (str.substring (3,8)) // lo wo6 </script>
3, Slice
Definition and usage: the slice () method extracts a portion of a string and returns the extracted part with a new string.
Syntax format: stringobject.slice (start,end)
Parameters |
Description |
Start |
The starting subscript for the fragment to be extracted. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on. |
End |
The subscript immediately following the end of the fragment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string. |
Return value: A new string. Includes the string Stringobject all characters from start (including start) to end (not including end).
Description
(1) The methods of the String object Slice (), substring (), and substr () (not recommended) can return the specified part of the string. Slice () is more flexible than substring () because it allows negative numbers to be used as arguments. Slice () and substr () are not
Because it specifies a substring with a position of two characters, and substr () specifies a substring with a character position and length.
(2) It is also important to note that String.slice () is similar to Array.slice ().
Example:
1 <script type= "Text/javascript" >2 var str = "Hellohappyworld"; 3 // 4 console.log (Str.slice (5)); // Happy World 5 Console.log (Str.slice (5,10)); // Happy 6 </script>
A summary of the processing of string interception by Java and JavaScript