Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of c# String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()

Tags:

java

string

c#

Working in c# i've found very useful two static methods of the String class :

  • IsNullOrEmpty()
  • IsNullOrWhiteSpace()

i can't find a valid surrogate in Java, is there something similar ?

Actually i have translated the two methods in this way :

public static boolean isNullOrEmpty(String a) { return a == null || a.isEmpty(); }   public static boolean isNullOrWhiteSpace(String a) { return a == null || (a.length() > 0 && a.trim().length() <= 0); } 

Is this the best way to translate these methods in Java ? What is the best way to translate these two methods in Java ?

like image 788
aleroot Avatar asked Dec 12 '11 15:12

aleroot


People also ask

What is C code in Java?

C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).

Are C and Java syntax similar?

KEY DIFFERENCES: C is a Procedural Programming Language whereas Java is an Object-Oriented language. C is middle level language while Java is high level language. C does not support threading on the other hand Java has a feature of threading. C supports pointers but Java does not support pointers.

Is Java more efficient than C?

Java is compiled into a lower language, then interpreted. It also has automatic garbage collection, and it's farther from machine code in the first place. Because of this C code tends to run faster than Java, but difference depends on what's being done and how well the code has been optimized.

Can we use c in Java?

It is a platform dependent intermediate machine code which will be converted to exe and then executed. Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa. Using JNI a java program has the capability to call the native C code.


1 Answers

I would prefer not to use String.trim to check for existence of whitespace. It is doing more work than you need, since it checks both ends of the string (even if non-whitespace was found at the other end) AND it returns a new String object. So I would prefer to implement a method to check for whitespace only.

So my suggestion (if implementing yourself) would be as follows:

public static boolean isNullOrEmpty(String s) {     return s == null || s.length() == 0; }  public static boolean isNullOrWhitespace(String s) {     return s == null || isWhitespace(s);  } private static boolean isWhitespace(String s) {     int length = s.length();     if (length > 0) {         for (int i = 0; i < length; i++) {             if (!Character.isWhitespace(s.charAt(i))) {                 return false;             }         }         return true;     }     return false; } 

Or taking a cue from String.trim's implementation, you could use character comparison rather than Character.isWhitespace():

// checking for whitespace like String.trim() does private static boolean isWhitespace(String s) {     int length = s.length();     if (length > 0) {         for (int i = 0; i < length; i++) {             if (s.charAt(i) > ' ') {                 return false;             }         }         return true;     }     return false; } 

Finally, I'd consider checking both ends of the string in each iteration, stepping inwards. This would minimize the number of iterations needed to get the answer, regardless of whether whitespace exists at the front or the end of the string.

private static boolean isWhitespace(String s) {     int length = s.length();     if (length > 0) {         for (int start = 0, middle = length / 2, end = length - 1; start <= middle; start++, end--) {             if (s.charAt(start) > ' ' || s.charAt(end) > ' ') {                 return false;             }         }         return true;     }     return false; } 
like image 161
sudocode Avatar answered Sep 20 '22 11:09

sudocode