startsWith,endsWith的作用和用法

时间:2025-02-15 08:09:17
  • public class StringDemo{      
  •   
  • public static void main(String args[]){  
  •  String s1="this is my startsWith string" 
  •     String sd="startsWith" 
  •     if ((sd))   
  •   //startsWith()方法判断字符串s1是否从字符串sd开始  
  •     s1=(());  
  •     else  
  •   if((sd))   
  •    //endWith()方法判断字符串s1是否从字符串sd结尾  
  •         s1=(0,()-());  
  •         else  
  •          
  •    int index=(sd);   
  •    //indexOf()搜索字符或子字符串首次出现,这里的index等于11  
  •             if(index!=-1 
  •              
  •     String s2=(0,index);   
  •      //从字符串s1的首字符开始,取index个字符  
  •                 String s3=(index+());   
  •      //取字符串s1的第19个字符后面的字符串  
  •                 s1=s2+s3;  
  •               
  •              else  
  •     ("string \""+sd+"\" not found");  
  •     
  •          (s1);  
  •