java提供了一个内置的静态字符串。比较方法?

时间:2023-01-15 19:00:17

when comparing strings I prefer not to rely on instance methods lest the string on which the method is called happens to be null. In .NET I just use the static String.Compare(string, string, bool) method. does java provide a similar built-in "null-safe" string compare utility or do I have to implement my own?

在比较字符串时,我宁愿不依赖实例方法,以免调用方法的字符串恰好为空。在。net中,我只使用静态字符串。(字符串,字符串,bool)方法进行比较。java提供了类似的内置“null-safe”字符串比较工具吗?

7 个解决方案

#1


8  

No, JDK does not contain such utility. However there are several external libraries that do that. For example org.apache.commons.lang.StringUtils provides null-safe equals(String, String) and equalsIgnoreCase(String, String). Guava has similar utilities too.

不,JDK不包含这样的实用程序。但是有几个外部库可以做到这一点。例如,org.apache.common .lang. stringutils提供了null-safe equals(String, String)和equalsIgnoreCase(String, String)。番石榴也有类似的效用。

#2


2  

There isn't. Apache Commons' StringUtils.equals() does it this way:

没有。Apache Commons' StringUtils.equals()这样做:

public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

#3


2  

String does offer a compareTo method, which is anyway not null-safe (i.e. you need to check for null value before actually calling the comapreTo method on the object).

String确实提供了一个compareTo方法,不管怎么说,它都不是null-safe(也就是说,在调用comapreTo方法之前,您需要检查空值)。

What you can do is write your own Comparator like this

你能做的就是像这样写你自己的比较器

class StringComparator implements Comparator<String> {

    public int compare(String s1, String s2) {
        if (s1 != null) {
            return s1.compareTo(s2);
        } else {
            if (s2 == null) {
                return 0;
            } else {
                return -1;
            }
    } 

To verify the property just do:

验证该属性是否正确:

Comparator<String> comp = new Comparator<String>();
System.out.println(comp.compare(s1, s2);

I didn't actually test the code, so take it as a general example. The benefit is that, even though you need to check for null value anyway, you can write it once and use it several times.

我实际上并没有测试代码,所以把它作为一个一般的例子。这样做的好处是,即使您需要检查null值,您也可以编写一次,并多次使用它。

You can also take a look at a built in Comparator inside the String class.

您还可以查看字符串类中内置的Comparator。

#4


1  

I guess the answer is no, you have probably to check it before

我想答案是否定的,你可能要先检查一下。

if(myString != null && myString.equals("string")) {
    // do something
}

Here some more examples

这里更多的例子

As others pointed out Apache Commons has this, and also Spring:

正如其他人指出的那样,Apache Commons有这一点,而且Spring:

StringUtils.hasLength()

StringUtils.hasLength()

if(StringUtils.hasLength(myString)) {
    // do something
}

#5


1  

There's a similar question here How to simplify a null-safe compareTo() implementation?

这里有一个类似的问题,如何简化一个null-safe compareTo()实现?

which sagely advises to use the Apache Commons ObjectUtils class

哪个明智地建议使用Apache Commons ObjectUtils类

result = ObjectUtils.compare(left, right);

#6


1  

If one of the strings to compare is a constant than you can use equals as it handles null.

如果要比较的字符串之一是常量,则可以使用equals,因为它处理null。

if("string".equals(mystring)) {
    // do something
}

If you have 2 variables then you can write your own static method:

如果你有两个变量,你可以写你自己的静态方法:

public static boolean stringEquals(String s1, String s2) {
    if (s1 == null) {
        return s2 == null;
    }
    return s1.equals(s2);
}

#7


-2  

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html compareTo(以)

There is the compare to which returns an int. There is also equals which returns a boolean. If you have a string instance it should be able to execute. If you pass instance.equals(null) it will return false. if your instance is null then you will get a null pointer.

这里有一个返回int的比较,也有返回一个布尔值的equals。如果您有一个字符串实例,它应该能够执行。如果您传递instance.equals(null),它将返回false。如果实例为空,则会得到一个空指针。

public class App
{

public static void main( String[] args )
{
    String myString = "test";

    if(myString.equals(null))
    {
        System.out.println("Its NUll");
    }else
    {
        System.out.println("Not Equal");
    }

}

}

#1


8  

No, JDK does not contain such utility. However there are several external libraries that do that. For example org.apache.commons.lang.StringUtils provides null-safe equals(String, String) and equalsIgnoreCase(String, String). Guava has similar utilities too.

不,JDK不包含这样的实用程序。但是有几个外部库可以做到这一点。例如,org.apache.common .lang. stringutils提供了null-safe equals(String, String)和equalsIgnoreCase(String, String)。番石榴也有类似的效用。

#2


2  

There isn't. Apache Commons' StringUtils.equals() does it this way:

没有。Apache Commons' StringUtils.equals()这样做:

public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

#3


2  

String does offer a compareTo method, which is anyway not null-safe (i.e. you need to check for null value before actually calling the comapreTo method on the object).

String确实提供了一个compareTo方法,不管怎么说,它都不是null-safe(也就是说,在调用comapreTo方法之前,您需要检查空值)。

What you can do is write your own Comparator like this

你能做的就是像这样写你自己的比较器

class StringComparator implements Comparator<String> {

    public int compare(String s1, String s2) {
        if (s1 != null) {
            return s1.compareTo(s2);
        } else {
            if (s2 == null) {
                return 0;
            } else {
                return -1;
            }
    } 

To verify the property just do:

验证该属性是否正确:

Comparator<String> comp = new Comparator<String>();
System.out.println(comp.compare(s1, s2);

I didn't actually test the code, so take it as a general example. The benefit is that, even though you need to check for null value anyway, you can write it once and use it several times.

我实际上并没有测试代码,所以把它作为一个一般的例子。这样做的好处是,即使您需要检查null值,您也可以编写一次,并多次使用它。

You can also take a look at a built in Comparator inside the String class.

您还可以查看字符串类中内置的Comparator。

#4


1  

I guess the answer is no, you have probably to check it before

我想答案是否定的,你可能要先检查一下。

if(myString != null && myString.equals("string")) {
    // do something
}

Here some more examples

这里更多的例子

As others pointed out Apache Commons has this, and also Spring:

正如其他人指出的那样,Apache Commons有这一点,而且Spring:

StringUtils.hasLength()

StringUtils.hasLength()

if(StringUtils.hasLength(myString)) {
    // do something
}

#5


1  

There's a similar question here How to simplify a null-safe compareTo() implementation?

这里有一个类似的问题,如何简化一个null-safe compareTo()实现?

which sagely advises to use the Apache Commons ObjectUtils class

哪个明智地建议使用Apache Commons ObjectUtils类

result = ObjectUtils.compare(left, right);

#6


1  

If one of the strings to compare is a constant than you can use equals as it handles null.

如果要比较的字符串之一是常量,则可以使用equals,因为它处理null。

if("string".equals(mystring)) {
    // do something
}

If you have 2 variables then you can write your own static method:

如果你有两个变量,你可以写你自己的静态方法:

public static boolean stringEquals(String s1, String s2) {
    if (s1 == null) {
        return s2 == null;
    }
    return s1.equals(s2);
}

#7


-2  

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html compareTo(以)

There is the compare to which returns an int. There is also equals which returns a boolean. If you have a string instance it should be able to execute. If you pass instance.equals(null) it will return false. if your instance is null then you will get a null pointer.

这里有一个返回int的比较,也有返回一个布尔值的equals。如果您有一个字符串实例,它应该能够执行。如果您传递instance.equals(null),它将返回false。如果实例为空,则会得到一个空指针。

public class App
{

public static void main( String[] args )
{
    String myString = "test";

    if(myString.equals(null))
    {
        System.out.println("Its NUll");
    }else
    {
        System.out.println("Not Equal");
    }

}

}