Java String Parsestring

Java String Parsestring | Parsing is the method of taking a string and extracting some information from it. In detail parsing is converting the string data into the other type, usually in java string parsing is done by using wrapper classes and the split method.

Let us see the different methods to parse the string.
1. Using a split() method to parse a string.
2. Using scanner class to parse a string

String Parsestring Java Using split()

The split() is a method of string that helps to split the string at the desired position without modifying the original string. There are two variants in the split() method:-
1. split(String regex)
2. split(String regex, int limit)

Observe the below code we have used both the variants of the split() method.

public class Main {
   public static void main(String args[]) {
      String str = "May272025";
      String[] arr = str.split("\\d+");
      for (String str1 : arr) {
         System.out.println(" Output1 : " + str1);
      }
      String str1 = "950-003-123-900-456 : 11 _343-1 789----";
      String[] arr1 = str1.split("-", 4);
      for (String str2 : arr1) {
         System.out.println(" Output2 : " + str2);
      }
   }
}

Output:-

Output1 : May
Output2 : 950
Output2 : 003
Output2 : 123
Output2 : 900-456 : 11 _343-1 789—-

Let us see another example of string parsestring Java. Here we will split it into an array based on some delimiter.

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      System.out.println("String Parsing Example #1: ");
      String string1 = new String("Everything is possible "
            + "in this world but we need to have courage");
      String delim = " ";
      String[] array1 = string1.split(delim);
      System.out.println(Arrays.toString(array1));

      System.out.println("\nString Parsing Example #2: ");
      String string2 = new String("Robert, California, 10, 34");
      String delims = "[,]+";
      String[] arr2 = string2.split(delims);
      System.out.println(Arrays.toString(arr2));

      System.out.println("\nString Parsing Example #3: ");
      String string3 = new String("Necessicity is the mother of invention");
      String delims1 = "[.,?!]+";
      String[] array3 = string3.split(delims1);
      System.out.println(Arrays.toString(array3));
   }
}

Output:-

String Parsing Example #1:
[Everything, is, possible, in, this, world, but, we, need, to, have, courage]

String Parsing Example #2:
[Robert, California, 10, 34]

String Parsing Example #3:
[Necessicity is the mother of invention]

Java String Parsestring using Scanner

import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      String string = "Necessicity is the mother of invention";
      Scanner scan = new Scanner(string);
      scan.useDelimiter("is");
      while (scan.hasNext()) {
         System.out.println("Output: " + scan.next());
      }
      scan.close();
   }
}

Output:-

Output: Necessicity
Output: the mother of invention

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.