Working in c# i've found very useful two static methods of the String class :
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 ?
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).
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With