Check if a string is blank or doesn't exist in Scala

java check if string is empty or whitespace
check if string is empty java
how to check if a string is empty c++
how to check if string is empty python
check if string is null java
java 8 string null or empty
java check if object is null or empty
check if string is empty javascript

I have an Option[String].

I want to check if there is a string exists and if it's exists its not blank.

def isBlank( input : Option[String]) : Boolean = 
{ 
     input.isEmpty || 
     input.filter(_.trim.length > 0).isEmpty 
}

Is there is a better way of doing this in Scala ?

What you should do is check using exists. Like so:

myOption.exists(_.trim.nonEmpty)

which will return True if and only if the Option[String] is not None and not empty.

Right way to check if String is empty in Java, The String may be null or empty, when the word to parse does not match. So, I wrote it like this: if(string.equals(� In addition, the "" (empty string) is not equal to Blank. The following formula would always return false: "" = Blank() /* <-- Please type this formula within a Label control to test it.*/ The "" (empty string) is a string text value, but there is no character within it.

An approach based in pattern matching,

def isBlank( input : Option[String]) : Boolean = 
  input match {
    case None    => true
    case Some(s) => s.trim.isEmpty
  }

Checking if a string is empty or null in Java, To check is given string does not have even blank spaces, use String.isEmpty() method. 1. String isBlank() Method. This method returns true if the given string is � For checking if a string is empty, null or undefined I use: function isEmpty(str) { return (!str || 0 === str.length); } For checking if a string is blank, null or undefined I use: function isBlank(str) { return (!str || /^\s*$/.test(str)); } For checking if a string is blank or contains only white-space:

This should work as well since filter of an empty Option results in an empty Option

def isBlank( input : Option[String]) : Boolean =  
   input.filter(_.trim.length > 0).isEmpty 

String isBlank(), isEmpty() method to check for an empty string in Java. To avoid NullPointerException in case the string is null, the method call should be preceded by a null check. I had the same problem and the second one doesn't work correctly. Try this: object x=3; bool result = string.IsNullOrEmpty(x as string); 'x as string' will be null so the result is true despite x having a value other than null or an empty string. I didn't find a short solution, used a double check. – molnarm Apr 17 '12 at 14:14

All proposed solutions will crash with NullPointerException if you pass:

val str : Option[String] = Some(null). 

Therefore null-check is a must:

def isBlank(input: Option[String]): Boolean = 
  input.filterNot(s => s == null || s.trim.isEmpty).isEmpty

Check if a string is empty or null in Java, However, the above program doesn't return empty if a string contains only whitespace characters (spaces). Technically, isEmpty() sees it contains spaces and� PS C:\> $DemoString. PS C:\> $DemoString -eq $null. True. PS C:\> $DemoString -eq "". False. You can see from this that $DemoString with no value assignment tests true for $null, but NOT for an empty string “”. To further confirm this, we can test $null against “” directly: PowerShell. PS C:\> $null -eq "" False.

exists (Accepted solution) will work when input has at least one element in it, that is Some("") but not when it's None.

exists checks if at least one element(x) applies to function.

eg.

scala> List[String]("apple", "").exists(_.isEmpty)
res21: Boolean = true

//if theres no element then obviously returns false
scala> List[String]().exists(_.isEmpty)
res30: Boolean = false

Same happens with Option.empty, as theres no element in it,

scala> Option.empty[String].exists(_.isEmpty)
res33: Boolean = false

So forall is what makes sure the the function applies all the elements.

scala> def isEmpty(sOpt: Option[String]) = sOpt.forall(_.trim.isEmpty)
isEmpty: (sOpt: Option[String])Boolean

scala> isEmpty(Some(""))
res10: Boolean = true

scala> isEmpty(Some("non-empty"))
res11: Boolean = false

scala> isEmpty(Option(null))
res12: Boolean = true

The gross way is to filter nonEmpty string, then check option.isEmpty.

scala> def isEmpty(sOpt: Option[String]) = sOpt.filter(_.trim.nonEmpty).isEmpty
isEmpty: (sOpt: Option[String])Boolean

scala> isEmpty(None)
res20: Boolean = true

scala> isEmpty(Some(""))
res21: Boolean = true

Java Program to Check if a String is Empty or Null, Why does if(char z=="") does not work? Give some condition to check the blank in string. Reply� In IF statement to evaluate whether the cell is Blank or Not Blank, you can use either of the following approaches; Logical expressions Equal to Blank (=””) or Not Equal to Blank (<>””) ISBLANK function to check blank or null values. If a cell is blank, then it returns TRUE, else returns FALSE.

5 ways to check if String is empty in Java - examples, Check out some simple ways in Java to test if a string is blank or empty. And also remember that Strings are immutable, so calling trim won't� -z is the second supported bash string comparison operator used to check if a string is empty or not. The -z operator functions similarly like -n operator. Below is an example: Most importantly, you should add spaces around the square brackets.

Checking for Empty or Blank Strings in Java, Use isEmpty() method available Java 6 onward to check if the String is so you won't be calling isEmpty() method on a null String if String itself� So the condition works when it's empty, but when it's not it fails because "The template language function empty expects its parameter to be and object, an array, or a string. The provided value is of type integer. Please see blah blah blah." So I need to change the data type or make it a string, that seems pretty hokeyI must be missing

Check if a String is Null or Empty in Java, To check if a string is null or empty in Java, use the == operator.Let's say we have the following strings.String myStr1 = Jack Sparrow; String� 1: If you just want to match " " as an empty string, then use the WHERE clause like this: WHERE DATALENGTH(COLUMN) > 0 . 2: But, if you want to count any string which consists of entire spaces as empty then use the WHERE clause like this : WHERE COLUMN <> '' The above code will not return NULL values when they are used in a WHERE clause.

Comments
  • I added a Scalafiddle to play with that: scalafiddle.io/sf/MLNE94O/10
  • That proves that the checked answer is not correct.
  • this is not correct : scala> val myOption: Option[String] = None myOption: Option[String] = None scala> myOption.exists(_.trim.nonEmpty) res1: Boolean = false
  • As I understand he want it to be True on None. I like your short code, but it's exactly the inverse. Should be - myOption.forall(_.trim.isEmpty)
  • That also works. I avoid using forall in example code here due to the non-obvious implications on None.
  • This fails with null value. Any ideas how to set it up with possible null as well?
  • @KapilMalhotra if you have a Some(null) you're doing Option wrong. The Option constructor will automatically convert to a None if you use it. If, instead, you're passing a value into the Some constructor, you lose that safety guarantee.
  • But you should never be passing Some(null). It destroys the benefit of using Option!
  • It is public method and you never know, how someone might misbehave with your provided contract. Therefore, I think, it is always good to double check and not to step on nullPointer.
  • Nah, just change exists(_.trim.nonEmpty) to forall(_.trim.isEmpty).
  • hoho, you are magic. I went the stupid route :) Thanks, updated answer
  • you can use implicit class btw: implicit class StringExtensions(val input: String) { def isNullOrEmpty: Boolean = input == null || input.trim.isEmpty }
  • This addresses a String and not an Option[String] as requested. Also, an empty string, "", is not the same as a "blank" string (i.e. whitespace).