Skip to main content

Posts

Showing posts from April, 2023

jdk 21: The new features in Java 21

JDK 21 is scheduled to be released in September 2023 as specified in    JSR 395 . The main 3 features included in JDK 21  are  a) Sequenced Collections According to  OpenJDK , a sequenced collection has first and last elements, and the elements between them have successors and predecessors. A sequenced collection supports common operations at either end, and it supports processing the elements from first to last and from last to first (i.e., forward and reverse). b)  Virtual Threads According to OpenJDK ,  Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. c)  String Templates (Preview)  According to OpenJDK ,  Enhance the Java programming language with  string templates . String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and  template processors  to produce specialized results

Java: How to split a string based on . (DOT) , (COMMA) : (COLON)

In Java language, DOT is a special character in the regular expression. If you have the following string String str = "He is a boy. He is my friend"; The following statement will return an empty string array String tokens[] = s.split("."); Solution: You can split a string based on DOT in the following two ways a) String tokens[] = str.split("[.]"); b) String tokens[] = str.split("\\."); The same solution also applies to the following characters a) WHITE SPACE  b) COMMA c) COLON Complete Example: public class StringSplit {      public static void main(String[] args ) {           String str = "He is a boy.He is my friend" ;           String tokens [] = str .split( "[.]" );           System. out .println( tokens [0]);           System. out .println( tokens [1]);          //or using escape character           tokens = str .split( "\\." );           System. out .println( tokens [0])