Summary of usage of string function substring in Java

Source: Internet
Author: User

This article is mainly about the use of string function substring in Java is introduced in detail, the need for friends can come to the reference, I hope to help you

String str; 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;

Demo

Copy CodeThe code is as follows: Class Test {public static void main (string[] args) {String S1 = "1234567890ABCDEFGH";   S1 = s1.substring (10);  System.out.println (S1); } }

Running Result: ABCDEFGH

Copy CodeThe code is as follows: Class Test {public static void main (string[] args) {String S1 = "1234567890ABCDEFGH";   S1 = s1.substring (0,9);  System.out.println (S1); } }

Run Result: 123456789

Here is a typical example:

Copy CodeThe code is as follows: 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); } }

Run Result: This is my string

Summary of usage of string function 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.