java 字符串截取

一:获取字符串索引

    String ss =“12323,sdfdf”;

    int i   =ss. indexOf(",");

1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。 

2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。 
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

二:根据索引截取字符串

    String ss =“12323,sdfdf”;

     int i   =ss.substring(0);

   1:  String  substring(int begingIndex);    //从索引出开始截取

   2:  String  substring(int begingIndex,int  endIndex);  //截取索引间字符串


  

注意: 1:  indexOf方法中如果没有找到字符的索引会返回-1

           2:substring()参数不能为负数,否则会报错

    

猜你喜欢

转载自blog.csdn.net/yuzhoaheyou/article/details/80733074