Java indexOf(String str)-将字符串与其他字符串相等
我需要编写一个方法,在其他字符串上检查“stringstr”,并返回str启动的索引 这听起来像是家庭作业,这是家庭作业的一部分,但我需要为考试学习 我试过:Java indexOf(String str)-将字符串与其他字符串相等,java,arrays,for-loop,Java,Arrays,For Loop,我需要编写一个方法,在其他字符串上检查“stringstr”,并返回str启动的索引 这听起来像是家庭作业,这是家庭作业的一部分,但我需要为考试学习 我试过: public int IndexOf (String str) { for (i= 0;i<_st.length();i++) { if (_st.charAt(i) == str.charAt(i)) { i++; if (_st.charAt(i) =
public int IndexOf (String str) {
for (i= 0;i<_st.length();i++)
{
if (_st.charAt(i) == str.charAt(i)) {
i++;
if (_st.charAt(i) == str.charAt(i)) {
return i;
}
}
}
return -1;
}
public int IndexOf(String str){
对于(i=0;i您正在使用i
使两个字符串相等,但您不希望的是第一个始终从0开始的字符串,除非找到的字符是另一个字符串。然后检查下一个字符是否相等,依此类推
希望这有帮助您正在使用i
使两个字符串相等,但您不希望的是第一个始终从0开始的字符串,除非找到的字符是另一个字符串。然后检查下一个字符是否相等,依此类推
希望这能帮上忙。恐怕这段代码的作用不大。它基本上是检查两个字符串在任何一点上是否有两个字符在同一位置,如果有,返回第二个字符的索引。例如,如果\u str
是“abcdefg”,而str
是“12cd45”,您将返回3,因为它们在同一个位置有“cd”,这是“d”的索引。至少,这是我所能知道的最接近的。这是因为您使用相同的索引变量对两个字符串进行索引
要重新写入indexOf
,在\u st
中查找str
,您必须扫描\u st
中的第一个字符,然后检查其余字符是否匹配;如果不匹配,则从开始检查的位置向前跳一步,然后继续扫描。(你可以做一些优化,但这是它的精髓。)例如,如果你在\u st
和str
的索引4中找到str
的第一个字符是六个字符长,找到第一个字符后,你需要看看剩下的五个字符(包括str
的索引1-5)是否有匹配\u st
的索引5-10(包括5-10)(最简单的方法是将str
的所有六个字符与\u st
的子字符串从4开始到六个字符ESR进行检查)。如果所有内容都匹配,则返回找到第一个字符的索引(在该示例中为4)。您可以在\u st.length()处停止扫描-str.length()
因为如果您在该点之前没有找到它,那么您将根本找不到它
旁白:不要在每个循环中调用length
函数。JIT可能能够优化调用,但如果您知道\st
在该函数执行过程中不会发生变化(如果您不知道,您应该需要它),请抓取length()
到本地文件,然后引用该文件。当然,因为您知道可以在length()之前停止
,您将使用一个local来记住您可以停在哪里。恐怕不是很近。该代码基本上是在那里检查两个字符串在任何点上是否有两个字符在相同的位置,如果是,则返回第二个字符的索引。例如,如果\u str
是“abcdefg”,而str
是“12cd45”,你会返回3,因为它们在同一个地方有“cd”,这是“d”的索引。至少,这是我能知道它实际在做什么。这是因为你用相同的索引变量对两个字符串进行索引
要重新写入indexOf
,在\u st
中查找str
,您必须扫描\u st
中的第一个字符,然后检查其余字符是否匹配;如果不匹配,则从开始检查的位置向前跳一步,然后继续扫描。(你可以做一些优化,但这是它的精髓。)例如,如果你在\u st
和str
的索引4中找到str
的第一个字符是六个字符长,找到第一个字符后,你需要看看剩下的五个字符(包括str
的索引1-5)是否有匹配\u st
的索引5-10(包括5-10)(最简单的方法是将str
的所有六个字符与\u st
的子字符串从4开始到六个字符ESR进行检查)。如果所有内容都匹配,则返回找到第一个字符的索引(在该示例中为4)。您可以在\u st.length()处停止扫描-str.length()
因为如果您在该点之前没有找到它,那么您将根本找不到它
旁白:不要在每个循环中调用length
函数。JIT可能能够优化调用,但如果您知道\st
在该函数执行过程中不会发生变化(如果您不知道,您应该需要它),请抓取length()
到本地文件,然后引用该文件。当然,因为您知道可以在length()之前停止
,您将使用一个local来记住您可以停止的位置。您的代码循环搜索正在搜索的字符串,如果位置i处的字符匹配,它将检查下一个位置。如果字符串在下一个位置匹配,则假定字符串str包含在_st中
您可能想做的是:
- 跟踪整个str是否包含在\u st中。您可能会检查您正在搜索的字符串的长度是否等于到目前为止匹配的字符数
- 如果您执行上述操作,那么您可以通过从i的当前值减去到目前为止的匹配数来获得起始索引。
一个问题:
为什么不使用内置的String.IndexOf()函数?此赋值是为了让您自己实现此功能吗?您的代码循环搜索正在搜索的字符串,如果位置i处的字符匹配,它将检查下一个位置。如果字符串在下一个位置匹配,则假定
/**
* Returns the index within this string of the first occurrence of the
* specified substring. The integer returned is the smallest value
* <i>k</i> such that:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* is <code>true</code>.
*
* @param str any string.
* @return 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, <code>-1</code> is returned.
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* 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 <tt>k</tt> for which:
* <blockquote><pre>
* k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* @param str the substring for which to search.
* @param fromIndex the index from which to start the search.
* @return the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, offset, count,
str.value, str.offset, str.count, fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
Loop master string
for every character (using index i, lets say)
check whether this is same as first character of the other string
if it is
//potential match
loop through the characters in the child string (lets say using index j)
match them with the consecutive characters in the master string
(something like master[j+i] == sub[j])
If everything match, 'i' is what you want
otherwise, continue with the master, hoping you find a match