0
public class fake {
    public static void main (String[] args) {
      String s = "i.like.this.program.very.much";
      String arr[] = s.split(".");
      int n = arr.length;
              
      System.out.println(n);
    }

my output is coming '0'

Nick Parsons
  • 38,409
  • 6
  • 31
  • 57

1 Answers1

2

You need to escape the dot (.) character, And why you need to do this is because(.) is the special character in Java Regular Expression

          String s = "i.like.this.program.very.much";
          String arr[] = s.split("\\.");
          int n = arr.length;
                  
          System.out.println(n);

Another way could be :

      String s = "i.like.this.program.very.much";
      String arr[] = s.split("[.]");
      int n = arr.length;
              
      System.out.println(n);
Harmandeep Singh Kalsi
  • 3,215
  • 2
  • 13
  • 22