Java code
- If (A. startswith (B ))
- // Judge whether string a starts with string B.
Java code
- If (A. endswith (B ))
- // Judge whether string a ends with string B.
A demo
From: http://hi.baidu.com/fsgs/blog/item/304a3687178f612cc75cc3fb.html
Author:Sdgqlyxq
Java code
- Public class stringdemo {
- Public static void main (string ARGs []) {
- String S1 = "this is my startswith string ";
- String SD = "startswith ";
- If (s1.startswith (SD ))
- // Startswith () method to determine whether string S1 starts from string SD
- S1 = s1.substring (SD. Length ());
- Else
- If (s1.endswith (SD ))
- // Use the endwith () method to determine whether string S1 ends with string SD.
- S1 = s1.substring (0, s1.length ()-SD. Length ());
- Else
- {
- Int Index = s1.indexof (SD );
- // Indexof () indicates the first occurrence of a character or substring. The index here is equal to 11.
- If (index! =-1)
- {
- String S2 = s1.substring (0, index );
- // Starts from the first character of string S1 and takes the index character
- String S3 = s1.substring (index + SD. Length ());
- // Obtain the string following the 19th characters of string S1
- S1 = S2 + S3;
- }
- Else
- System. Out. println ("string \" "+ SD +" \ "not found ");
- }
- System. Out. println (S1 );
- }
- }
From: http://heisetoufa.iteye.com/blog/230328