使用 charAt 和 substring 的不同输出
Different output using charAt and substring
对编程来说还是个新手,我正在通过一些练习来理解基础知识。这是我的作业:
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the
second only if it is 'z', so "ozymandias" yields "oz".
startOz("ozymandias") → "oz" startOz("bzoo") → "z" startOz("oxx") →
"o"
我已经看过解决方案并且确实理解它,但无法弄清楚为什么我自己尝试使用 substring 而不是 'charAt 会生成不同的输出。为什么我自己使用子字符串的 code1 与我使用“charAt”时的输出不同? code1是我自己的尝试,code2是给出的方案。在附件中,您将找到两个输出。谢谢!
//code 1 own attempt
public String startOz(String str) {
String answer = "";
if ( str.length() >= 1 && str.substring( 0 ).equals("o")) {
answer = answer + str.substring(0);
}
if ( str.length() >= 2 && str.substring( 1 ).equals("z")) {
answer = answer + str.substring(1);
}
return answer;
}
//code 2 the solution
public String startOz(String str) {
String answer = "";
if ( str.length() >= 1 && str.charAt( 0 ) == 'o') {
answer = answer + str.charAt(0);
}
if ( str.length() >= 2 && str.charAt( 1 ) == 'z') {
answer = answer + str.charAt(1);
}
return answer;
}
阅读 String#substring
上的 javadocs - 您需要添加一个额外的参数来指定子字符串的结尾,否则 returns 字符串的其余部分。
这是 substring(int index)
的文档
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring
begins with the character at the specified index and extends to the
end of this string.
所以第一个 if
你得到 ozymandias
并且它不等于 o
。
正确的是使用:
substring(int beginIndex, int endIndex)
文档:
public String substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string. The substring
begins at the specified beginIndex and extends to the character at
index endIndex - 1. Thus the length of the substring is
endIndex-beginIndex.
Link: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring%28int%29
对编程来说还是个新手,我正在通过一些练习来理解基础知识。这是我的作业:
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz". startOz("ozymandias") → "oz" startOz("bzoo") → "z" startOz("oxx") → "o"
我已经看过解决方案并且确实理解它,但无法弄清楚为什么我自己尝试使用 substring 而不是 'charAt 会生成不同的输出。为什么我自己使用子字符串的 code1 与我使用“charAt”时的输出不同? code1是我自己的尝试,code2是给出的方案。在附件中,您将找到两个输出。谢谢!
//code 1 own attempt
public String startOz(String str) {
String answer = "";
if ( str.length() >= 1 && str.substring( 0 ).equals("o")) {
answer = answer + str.substring(0);
}
if ( str.length() >= 2 && str.substring( 1 ).equals("z")) {
answer = answer + str.substring(1);
}
return answer;
}
//code 2 the solution
public String startOz(String str) {
String answer = "";
if ( str.length() >= 1 && str.charAt( 0 ) == 'o') {
answer = answer + str.charAt(0);
}
if ( str.length() >= 2 && str.charAt( 1 ) == 'z') {
answer = answer + str.charAt(1);
}
return answer;
}
阅读 String#substring
上的 javadocs - 您需要添加一个额外的参数来指定子字符串的结尾,否则 returns 字符串的其余部分。
这是 substring(int index)
的文档public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
所以第一个 if
你得到 ozymandias
并且它不等于 o
。
正确的是使用:
substring(int beginIndex, int endIndex)
文档:
public String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is
endIndex-beginIndex.
Link: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring%28int%29