Usage of substring in Java

Source: Internet
Author: User

Usage of substring in Java

str=str.substring (int beginindex), intercept str from the initial letter length of Beginindex string, the remaining string is assigned to STR;

str=str.substring (int beginindex,int endIndex), intercepts the string from Beginindex to EndIndex at the end of STR and assigns it to STR;

Here is a demo program:

public class stringdemo{

public static void Main (String agrs[]) {

String Str= "This is my original string";

String todelete= "original";

if (Str.startswith (Todelete))

Str=str.substring (Todelete.length ());

Else

if (Str.endswith (Todelete))

Str=str.substring (0, Str.length ()-todelete.length ());

Else

{

int Index=str.indexof (todelete);

if (index!=-1)

{

String str1=str.substring (0, index);

String str2=str.substring (Index+todelete.length ());

STR=STR1+STR2;

}

Else

System.out.println ("string/" "+todelete+"/"Not Found");

}

System.out.println (str);

}

}

(Original quote from:http://hi.baidu.com/ccsos/blog/item/42ff84afe6e62bcd7dd92a62.html)

Supplement: str=str.substring (int beginindex,int endIndex); The resulting value:

Beginindex =< str Values < EndIndex

The above supplement is my own previous understanding of the comments in the API recently, and publish it below so that more and I like beginners better understanding of the above program substring
Public  substring(int beginindex,                        int endIndex)
Returns a new string that is a substring of this string. The substring starts at the specified   beginindex   and continues to the character at index   endIndex-1 . Therefore, the length of the substring is   endindex-beginindex .

Example:

 "Hamburger". Substring (4, 8) returns "Urge" "smiles". Substring (1, 5) returns "Mile" 

 

parameters:
Begininde x the index (including) at the beginning of the  -. The index (not included) at the end of the
endIndex  -.
returns:
the specified substring.
Thrown:
indexoutofboundsexception  -if   beginindex   Negative, or   endIndex   greater than this   string   The length of the object, or   beginindex   greater than   EndIndex .

Usage of substring in Java

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.