java如何读取字符串中的某一段字符串

如题所述

可以用substring方法来实现。

参考代码:

String str = "hello word!";
         System.out.println(str.substring(1,4));
         System.out.println(str.substring(3,5));
   System.out.println(str.substring(0,4));
将得到结果为:
        ell
        lo 
        hell
如果startIndex和endIndex其中有越界的将会抛出越界异常。

String.substring(int beginIndex, int endIndex)

参数:

beginIndex 开始位置索引

endIndex    ç»“束位置索引

返回:

从beginIndex位置到endIndex位置内的字符串

温馨提示:答案为网友推荐,仅供参考
相似回答