Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Split() in Java

 
Split in Java

In our earlier tutorial we have seen about String Tokenizer class in Java. And we have given Split method from String class which will be an alternate since String Tokenizer is legacy class. So its recommended to use split method from String class instead of StringTokenizer. So lets see whats the use if split method and how we can use in Java by using simple example. 

Split method used to search for the match as specified in the argument and splits the string and stores into an String array. There are 2 types of split methods in String class,

String str = "Hello how are you";
String arr[] = str.split(" ");

will give output as below, since we are splitting by space " "
Hello
how
are
you



String str = "Hello how are you";
String arr[] = str.split(" ", 2);

will give output as below, here we are requesting to split the string with the array size as 2. so remaining complete string will be stored in last array as below
Hello
how are you


Lets see simple java example with their output.


public class SplitTest {
 
 public static void main(String[] args) {
  String str = "Hello how are you";
  
  String arr[] = str.split(" ");
  for (String string : arr) {
   System.out.println(string);
  }
  
  System.out.println("-------------------------------");
  
  String arr1[] = str.split(" ",3);
  for (String string : arr1) {
   System.out.println(string);
  }
 }
}


OUTPUT:


Hello
how
are
you
-------------------------------
Hello
how
are you






String Tokenizer in Java


String Tokenizer in Java

In most of the older application we will be seen String Tokenizer class used for operations like splitting words or text from a given String or line. The String Tokenizer class allows user to break a String into tokens based on the input parameters. By default String Tokenizer will split the String and gives the token based on space character else we can provide our delimit as parameter. Lets see few String Tokenizer constructor parameters with their output.

StringTokenizer tok = new StringTokenizer("Hello how are you");
will give output as below, since space (" ") is a default delimiter
Hello
how
are
you

StringTokenizer tok = new StringTokenizer("Hello|how|are|you", "|");
will give output as below, since we have delimit as "|"
Hello
how
are
you

StringTokenizer tok = new StringTokenizer("Hello|how|are|you", "|", true);
will give output as below, true gives the delimiter along with tokens 
Hello
|
how
|
are
|
you

Lets see simple example of using String Tokenizer class in Java.


import java.util.StringTokenizer;

public class StringTokenizerTest {
 
 public static void main(String[] args) {
  
  StringTokenizer tok = new StringTokenizer("Hello how are you");
  while(tok.hasMoreTokens()){
   System.out.println(tok.nextElement());
  }
  
  System.out.println("-------------------------------");
  
  tok = new StringTokenizer("Hello|how|are|you", "|");
  while(tok.hasMoreTokens()){
   System.out.println(tok.nextElement());
  }
  
  System.out.println("-------------------------------");

  tok = new StringTokenizer("Hello|how|are|you", "|", true);
  while(tok.hasMoreTokens()){
   System.out.println(tok.nextElement());
  }
 }
}


OUTPUT:


Hello
how
are
you
-------------------------------
Hello
how
are
you
-------------------------------
Hello
|
how
|
are
|
you




Alternate:

String Tokenizer is from legacy class and because of compatibility reasons recommended to use "split" method from String class. Lets see this "split" method in our next tutorial



Difference between String, StringBuffer and StringBuilder


In this tutorial we will see about difference between String, StringBuffer and StringBuilder. The most important difference that String Object is immutable but whereas StringBuffer/ StringBuilder are mutable. 

So what is immutable and mutable?
Once a value assigned to the String Object then the values can't be modified or changed is called as immutable class. On the other end the values can be changed and modified is called as mutable class.

How String works in Java?
Since String is immutable class we can't change the values once we placed in String Object. But we are able to modify the String value in Java and how its possible? Yes, whenever we modify a String Object new Object will be created. Below example will explain more details on how String works in Java.

String val = "My";
val = val + " Friend";
System.out.println(val);

Output of above code will be "My Friend". We assume that value of "val" gets concatenated. But internally Java creates Object for every String operation which we does. In Java we can create String Object by 2 ways like 

Using "new" operator:
String val = new String("My");

Using String Literal:
String val = "Friend";

Since String creates Objects at each time, we need to switch to StringBuffer or StringBuilder whenever we does String concatenations to avoid multiple String Objects creation and to increase our application performance.

Internally StringBuilder and StringBuffer or mutable, then what is the difference?
If we go back to JDK versions StringBuffer introduced in JDK 1.4 and StringBuilder introduced in JDK 1.5 version. 
Other main difference between StringBuffer and StringBuilder are synchronization. Internally StringBuffer is synchronized and StringBuilder is non-synchronized. Synchronized means it is thread safe and we can use when we implement any multi-threading operations, only one thread can modify StringBuffer at a time. Whereas StringBuilder is not thread safe.

Lets see how to create StringBuffer and StringBuilder

StringBuffer:
StringBuffer str = new StringBuffer();
str.append("My");
str.append(" Friend");
System.out.println(str);

StringBuilder:
StringBuilder str = new StringBuilder();
str.append("My");
str.append(" Friend");
System.out.println(str);