Summary:
Substring in Java and C #
If there is only one parameter, it means the same, take all the characters after the index
If there are two parameters. The second parameter of the substring in Java indicates the index number. The actual value is the first digit of the index number. The second parameter of the substring method in C # indicates the length of the substring.
Java API description:
Substring
Returns a new string that is a substring of this string. the substring begins at the specified beginindex and extends to the character at index endindex-1. thus the length of the substring is endindex-beginindex.
Examples:
"Hamburger". substring (4, 8) returns "urge"
"Smiles". substring (1, 5) returns "Mile"
Parameters:
Beginindex the beginning index, inclusive.
Endindex the ending index, exclusive.
For example
Public class testsubstring {public static void main (string [] ARGs) {string phonenbr = "05718888888"; // the four-digit area code should be substring (), not substring) system. out. println (phonenbr. substring (0, 4) + "" + phonenbr. substring (4 ));}}
========================================================== ====================
C #
Substring
- Public string substring (INT startindex)
Retrieves a substring from this instance. The substring starts at a specified character position.
Parameters
-
Startindex
-
Type: system. int32
The zero-based starting character position of a substring in this instance.
Return Value
Type: system. String
A string that is equivalent to the substring that begins at startindex in this instance, or empty if startindex is equal to the length of this instance.
- Public string substring (INT startindex, int length)
Parameters
-
Startindex
-
Type: system. int32
The zero-based starting character position of a substring in this instance.
-
Length
-
Type: system. int32
The number of characters in the substring.
Return Value
Type: system. String
A string that is equivalent to the substring of length that begins at startindex in this instance, or empty if startindex is equal to the length of this instance and length is zero.
String mystring = "ABC"; bool test1 = mystring. substring (2, 1 ). equals ("C"); // This is true. console. writeline (test1); bool Test2 = string. isnullorempty (mystring. substring (3, 0); // This is true. console. writeline (Test2); try {string str3 = mystring. substring (3, 1); // This throws argumentoutofrangeexception. console. writeline (str3);} catch (argumentoutofrangeexception e) {console. writeline (E. message );}