java中 IndexOf()、lastIndexOf()、substring()的用法

public int indexof(String str)返回字符串中出现str的第一个位置

public int indexof(String str,int fromIndex)返回字符串中从fromIndex开始出现str的第一个位置

public String substring(int beginIndex)返回从beginIndex开始的字符串

public String lastIndexOf(String str)返回从str最后一次出现的位置

如:

String pexfix = fileName.substring(
fileName.lastIndexOf("."),
fileName.length());

返回字符串pexfix中以.结束的位置到整个字符串结束之间的字符串,即获取后缀名

在java字符串中,对有特殊含义的字符要使用转义符转意。比如对",因为它表示字符串的开始和结束,所以对字符串中存在的"要使用转义符转义,比如字符串"hello"需要写成""hello""。
""是转义符,所以也是特殊意义的字符,它自己也要转义。所以要获得字符串"",需要写成"\",所以需要使用lastIndexOf("\")才能找出字符串中最后一个 。
原文地址:https://www.cnblogs.com/guweiwei/p/7374018.html