public int pos(String yrstr, char c) { char[] charr = yrstr.toCharArray(); for (int i=0; i<charr.length; i++) if (charr[i] == c) return i; // Return the position return -1; // No match }应该就是这样的啦
indexOf public int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: this.startsWith(str, k)
is true. Parameters: str - any string. Returns: if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned. Throws: NullPointerException - if str is null.--------------------------------------------------------------------------------indexOf public int indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value k for which: k >= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
If no such value of k exists, then -1 is returned. Parameters: str - the substring for which to search. fromIndex - the index from which to start the search. Returns: the index within this string of the first occurrence of the specified substring, starting at the specified index. Throws: NullPointerException - if str is null.
{
char[] charr = yrstr.toCharArray(); for (int i=0; i<charr.length; i++)
if (charr[i] == c)
return i; // Return the position return -1; // No match
}应该就是这样的啦
public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true. Parameters:
str - any string.
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
NullPointerException - if str is null.--------------------------------------------------------------------------------indexOf
public int indexOf(String str,
int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest value k for which:
k >= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
If no such value of k exists, then -1 is returned. Parameters:
str - the substring for which to search.
fromIndex - the index from which to start the search.
Returns:
the index within this string of the first occurrence of the specified substring, starting at the specified index.
Throws:
NullPointerException - if str is null.