在 Java 中不使用 String.compareTo 比较字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26578353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing Strings without using String.compareTo in Java
提问by John Cena
Basically, the assignment is being an ass and doesn't want us to use an existing method to compare two strings alphabetically.
基本上,分配是一个笨蛋,不希望我们使用现有方法按字母顺序比较两个字符串。
It should return 1 if the first string is "bigger" alphabetically than the second (in the sense that 'g' is bigger than 'a'), -1 if the second is bigger, or 0 if they are the same.
如果第一个字符串按字母顺序比第二个字符串“大”,则它应该返回 1(从某种意义上说,'g' 大于 'a'),如果第二个更大,则返回 -1,如果它们相同则返回 0。
Lets say i have
可以说我有
String a = "Cows";
String b = "Horses";
The method should return -1.
该方法应返回-1。
My understanding is to use a for() loop to scan both a and b, using charAt(), but I have no idea how to implement this...
我的理解是使用 for() 循环来扫描 a 和 b,使用 charAt(),但我不知道如何实现这一点......
EDIT***
编辑***
reading the answers I've come up with this.
阅读我提出的答案。
int compared = 0;
for (int i = 0; i<s1.length() && i<s2.length(); i++){
int a = s1.charAt(i);
int b = s2.charAt(i);
if(a < b){
compared = -1;
}
else if(a > b){
compared = 1;
}
}
return compared;
The strings being compared all start with uppercase, so it shouldn't be a problem. However, when using the normal String.compareTo() and a bubblesort method that counts the number of times this method was called while sorting a predertermined string array, I get different results, which means something is obviously wrong.
被比较的字符串都以大写开头,所以应该没有问题。但是,当使用普通的 String.compareTo() 和一个计算该方法在对预定字符串数组进行排序时被调用的次数的冒泡排序方法时,我得到了不同的结果,这意味着某些事情显然是错误的。
For people viewing this and having the same problem, here's how the code works
对于查看此内容并遇到相同问题的人,以下是代码的工作原理
int compared = 0;
//program assumes strings are equal
for (int i = 0; i<s1.length() && i<s2.length(); i++){
//for() loop goes on until the largest string
int a = s1.charAt(i);
int b = s2.charAt(i);
//convert char into int for comparison just in case
if(a < b){
compared = -1;
break;
//breaks at the first occurence of non equal characters
}
else if(a > b){
compared = 1;
break;
//same as above
}
}
return compared;
回答by Olavi Mustanoja
Without really giving you the answer, given this is homework, I think I can point you in to the right direction.
没有真正给你答案,鉴于这是家庭作业,我想我可以为你指明正确的方向。
Your understanding to use a for loop to iterate through the two words is correct, as well as using the String.charAt. Now what I want you to start with is to find the end of your for loop - that is, find out how many characters you should iterate through?
您对使用 for 循环遍历这两个词的理解是正确的,使用 String.charAt 也是如此。现在我想让你开始的是找到你的 for 循环的结尾——也就是说,找出你应该迭代多少个字符?
A hint:
一个提示:
'b' > 'a'
'b' > 'a'
This hint has a trap in it though. What should you do, given the following strings?
不过,这个提示有一个陷阱。鉴于以下字符串,您应该怎么做?
Haskell
java
哈斯克尔
爪哇
A hint for this too:
这也是一个提示:
'A' < 'a'
'A' < 'a'
回答by CMOS
Yes your idea is the right way.
Loop through the characters of both Strings while comparing them.
To compare cast the character you get as (int)
to get the ASCII code, then by comparing the ASCII code of each letter you can decide which one is >
<
or =
.
是的,你的想法是正确的。在比较它们的同时循环遍历两个字符串的字符。比较转换你得到的字符(int)
以获得 ASCII 码,然后通过比较每个字母的 ASCII 码,你可以决定哪个是>
<
或=
。
回答by JDForLife
public int strCompare(String a,String b)
{
int i=0;
while(i<a.length()&&i<b.length()&&(a.charAt(i)==b.charAt(i)))
{
i++;
}
if(i==a.length()&&i==b.length())
return 0;
if(i>=a.length())
return -1;
else if(i>=b.length())
return 1;
return a.charAt(i)-b.charAt(i);
}