Substring in Java and C #

Source: Internet
Author: User

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 );}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.