Startswith (), endswith (), usage, judge whether string a starts or ends with string B

Source: Internet
Author: User
Java code
  1. If (A. startswith (B ))
  2. // Judge whether string a starts with string B.
Java code
  1. If (A. endswith (B ))
  2. // Judge whether string a ends with string B.

 

A demo

From: http://hi.baidu.com/fsgs/blog/item/304a3687178f612cc75cc3fb.html

Author:Sdgqlyxq

 

Java code
  1. Public class stringdemo {
  2. Public static void main (string ARGs []) {
  3. String S1 = "this is my startswith string ";
  4. String SD = "startswith ";
  5. If (s1.startswith (SD ))
  6. // Startswith () method to determine whether string S1 starts from string SD
  7. S1 = s1.substring (SD. Length ());
  8. Else
  9. If (s1.endswith (SD ))
  10. // Use the endwith () method to determine whether string S1 ends with string SD.
  11. S1 = s1.substring (0, s1.length ()-SD. Length ());
  12. Else
  13. {
  14. Int Index = s1.indexof (SD );
  15. // Indexof () indicates the first occurrence of a character or substring. The index here is equal to 11.
  16. If (index! =-1)
  17. {
  18. String S2 = s1.substring (0, index );
  19. // Starts from the first character of string S1 and takes the index character
  20. String S3 = s1.substring (index + SD. Length ());
  21. // Obtain the string following the 19th characters of string S1
  22. S1 = S2 + S3;
  23. }
  24. Else
  25. System. Out. println ("string \" "+ SD +" \ "not found ");
  26. }
  27. System. Out. println (S1 );
  28. }
  29. }

From: http://heisetoufa.iteye.com/blog/230328

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.