Good morning,
This is for the newbies:
When you're developing code and you need to test if a String object is truly empty, be careful. Here's an example:
class IsStringTrulyEmpty {
public static void main(String[] args) {
//Is string truly empty?
String s = " ";
String t = "";
//Test
System.out.println("Is s truly empty? " + s.isEmpty());
System.out.println("Is t truly empty? " + t.isEmpty());
}
}
Here is the output when run:
Is s truly empty? false
Is t truly empty? t rue
Notice that s returned false. Why? There is a space between the quotation marks. This may have an impact, because if you're checking if a string is empty, it will never be true if there's a space.
It truly is all in the details.
Happy coding!
This is not an official Python page. It is simply a series of posts demonstrating code and other things to help other newbies to the Python language. Because I've worked with Java before, I'm drawn to the Java implementation of Python, Jython, so my posts will be more about that. But any non-Java related code should work in Python, especially if I discuss built-ins. Hope what I say is helpful. If there are mistakes, or I'm not clear on something, please let me know. Happy coding!
Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts
Tuesday, August 21, 2018
Subscribe to:
Posts (Atom)