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
  • 45,728
  • 6
  • 46
  • 64

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,315
  • 2
  • 14
  • 26