Java 11: New Methods in String Class


Java 11 doesn't have a lot of language unequivocal features. Along these lines, it was bewildering to see 6 new procedures getting introduced in Java String Clas.

Java 11 String Class New Methods

Let's look at these new String class procedures one by one.

  1. isBlank(): This procedure returns authentic if the string is unfilled or contains simply void regions code points.
  2.  
    
    String s = "abc"; 
    
    System.out.println(s.isBlank()); 
    
    s = ""; 
    
    System.out.println(s.isBlank()); 
    
    s = "\t \t"; 
    
    System.out.println(s.isBlank()); 
    
    

    Output:

     
    
    false 
    
    true 
    
    true 
    
    

    Notice that "\t" is considered as a void zone character codepoint in Unicode.

    java String IsBlank()

    I am using >jShell to execute the code bits without truly making a java file.

  3. lines(): This procedure reestablishes a surge of lines removed from the string, secluded by line eliminators, for instance, \n, \r etc.
  4.  
    
    String s1 = "Hi\nHello\rWassup"; 
    
    System.out.println(s1); 
    
    List lines = s1.lines().collect(Collectors.toList()); 
    
    System.out.println(lines); 
    
    

    Output:

    Java String lines() Function

    Java String lines() Function

    This technique is valuable to process multi-line strings with one line at a time.

  5. strip(), stripLeading(), stripTrailing(): These systems are used to strip whitespaces from the string. As the name proposes, strip() will remove driving and trailing whitespaces. In any case, stripLeading() will oust simply driving whitespaces and stripTrailing() will clear simply trailing whitespaces.
  6.  
    
    String s2 = " Hello, \tWorld\t "; 
    
    System.out.println("#" + s2 + "#"); 
    
    System.out.println("#" + s2.strip() + "#"); 
    
    System.out.println("#" + s2.stripLeading() + "#"); 
    
    System.out.println("#" + s2.stripTrailing() + "#"); 
    
    

    Output:

     
    
    # Hello, 	World	 # 
    
    #Hello, 	World# 
    
    #Hello, 	World	 # 
    
    # Hello, 	World# 
    
    
    Java String strip(), stripLeading(), stripTrailing() functions

    Java String strip(), stripLeading(), stripTrailing()

  7. repeat(int n): This technique restores another string whose esteem is the connection of this string rehashed 'n' times.
  8.  
    
    String s3 = "Hello\n"; 
    
    System.out.println(s3.repeat(3)); 
    
    s3 = "Do"; 
    
    System.out.println(s3.repeat(2)); 
    
    

    Output:

    Java String repeat()

    Java String repeat()

Conclusion

Java String class has a huge amount of utility procedures. Regardless, all these new utility strategies will be incredibly important since we won't have to worry over staying in contact with them ourselves and thinking about whether they spread all the phenomenal circumstances related to different sorts of Unicode characters or not.






© Journaldev Python 3 tutorial spring tutorial