像比较数字一样比较字符串

发布于 2024-10-06 01:43:23 字数 67 浏览 6 评论 0原文

我想知道是否可以像比较数字一样比较字符串。例如,有什么方法可以使 "Cat" > “狗”

I was wondering if it is possible to compare strings as if they were numbers. For instance is there any way you could make it so that "Cat" > "Dog"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

少年亿悲伤 2024-10-13 01:43:23

您不能按照您的建议使用运算符(例如 "Cat" < "Dog")。正如 @larsmans 所说,这将需要 Java 不提供的运算符重载。不过,您仍然可以使用 "Cat".compareTo("Dog") 比较字符串,如果字符串相等则返回 0,如果 "Cat" 则返回大于 0 的数字按字典顺序小于“Dog”,否则为负数。

请参阅此页面

You can't use operators (e.g. "Cat" < "Dog") as you suggest. As @larsmans says that would require operator overloading which Java doesn't provide. However, you can still compare strings using "Cat".compareTo("Dog") which returns 0 if the strings are equal, a number greater than 0 if "Cat" is lexicographically less than "Dog", or a negative number otherwise.

See this page

笑饮青盏花 2024-10-13 01:43:23

只需实现 Comparator 接口并以您喜欢的任何方式实现比较即可。

这是 Javadoc

Just implement the Comparator interface and implement the comparison any way you like.

Here is the Javadoc

旧城烟雨 2024-10-13 01:43:23

你不能。这需要 运算符重载,Java 不会让你

You can't. This would require operator overloading, which Java won't let you.

眼角的笑意。 2024-10-13 01:43:23

不,不存在。
您必须使用compareTo()。

Nope, doesn't exist.
You have to use compareTo().

浸婚纱 2024-10-13 01:43:23

您可以使用 String 中的 compareTo 方法

You can use the compareTo method in String

流心雨 2024-10-13 01:43:23

您需要使用如下方法:

int compare(String s1, String s2);   // write code to do comparison.

You need to use some method as below :

int compare(String s1, String s2);   // write code to do comparison.
葵雨 2024-10-13 01:43:23

由于执行此操作的唯一方法是为每个单词保留一个设定值,因此您将使用数组或某种其他形式的数据存储。

这是一个示例,您只需将单词及其对应的值保留在两个数组中(注意,它们的顺序必须相同,因此第一个单词对应于第一个数字,等等)。

public static String[] words = {"cat","dog","banana"};
public static int[] value = {3,4,5};
public static void main(String[] args){
    if(valOf("Cat") > valOf("Dog")){
        System.out.print("Cat is greater than Dog");
    }
    else{
        System.out.print("Cat is not greater than Dog");
    }
}
public static int valOf(String str){
    for(int x=0;x<words.length;x++){
        if(str.equalsIgnoreCase(words[x])){
            return value[x];
        }
    }
    return -1;
}

Since the only way to do this is to keep a set value for each word, you'd be using arrays, or some other form of data storage.

Here is an example where you just keep the words, and their corresponding values in two arrays (note, they must be in the same order, so the first word corresponds with the first number, etc).

public static String[] words = {"cat","dog","banana"};
public static int[] value = {3,4,5};
public static void main(String[] args){
    if(valOf("Cat") > valOf("Dog")){
        System.out.print("Cat is greater than Dog");
    }
    else{
        System.out.print("Cat is not greater than Dog");
    }
}
public static int valOf(String str){
    for(int x=0;x<words.length;x++){
        if(str.equalsIgnoreCase(words[x])){
            return value[x];
        }
    }
    return -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文