One of the most important topics in Java is String manipulation. Java String class includes several methods for you to to concatenate , replace an element , create a sub-string,…etc.For a Java newbie, its very important to grasp the concepts of String and learn all of its methods as there would hardly be any Java program that doesn’t make use of this class directly or indirectly.Today we’ll see two of the most common and important topics based on it - splitting and tokenizing the continuous sequence of characters . Below are the two simple programs that explains the logic of tokenizing and splitting -

Java String Split Example :-

class strTest {
String str=null;
public strTest (String s) // this is a parametrized constructor that accepts a string argument
{
this.str=s; //this line will initialize the str variable
}
protected void function1()
{
for(String s:str.split("Ja")) //this line will split the text
{
System.out.println(s);
}
}
}
public class strManipulation {
public static void main(String args[])
{
strTest stt=new strTest("AppleJavaComputerJaPramod"); // this line instantiates the class stTest
stt.function1(); // this line calls the function1 from the strTest class
}
}

Explanation of the above Java String Split Example :-

The strManipulation class is our main class in which we’ve instantiated the strTest class by passing a string .The strTest class has a parametrized constructor that accepts a string and initializes the str variable .

The function1() method in the strTest class makes use of the split() method (as its shown in the for each loop) which accepts a regex string pattern to match and split a particular string.

Click here to read more on the Split method.

 

Java StringTokenizer tutorial :-

import java.util.StringTokenizer;

class sStoken {

protected void function1(String s)
{
StringTokenizer sts=new StringTokenizer(s,” “);
System.out.println(s);
while(sts.hasMoreElements()) // this line will check if there are more tokens available
{
System.out.println(sts.nextElement()); // this line will print the discovered token
}
}
}
public class strToken {
public static void main(String args[])
{
sStoken ss=new sStoken();
ss.function1(“pramod choudhari owner of techwayz”); // this line passes a text to our function1 which generates token from the parameter passed to it.
}
}

Explanation of the above Java StringTokenizer tutorial :-

The strToken is the main class in our above example which instantiates the sStoken class using the its default constructor.The sStoken class has the method named function1() that accepts a string parameter and passes it as a parameter to the constructor of the StringTokenizer class. We then use the hasMoreElements() and the nextElement() collection’s method to iterate over the generated tokens.The hasMoreElements will keep returning true until it can’t find further elements. The nextElement () is a method in the Enumeration interface . If the hasMoreElements() method discovers new element , the nextElement method will return the discovered element.

These were just two simple examples . We’ll cover more examples in the coming days . I hope you’ve found these two simple examples useful .Kindly share this post with your friends if you’ve found these two tutorials useful.Kindly follow us on Facebook or twitter to stay updated with the latest Java tutorials.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
the author

I am a post graduate in computer sciences .I have 1+ year experience of working as a Java/J2EE developer in a top notch IT company. I love developing applications and writing technology related articles .

3 Readers Commented

Join discussion
  1. Obasi Miracle on September 24, 2013

    Hello Pramod,
    I am happy you are sharing this at a time it will be very useful for a project I am currently working on.
    As always you have been sharing great stuff, keep it up brother and do have a great week ahead
    Obasi Miracle recently posted…How to add Video Widget to Genesis Child Themes without using a pluginMy Profile

    • Pramod Author on September 24, 2013

      Hi Obasi !
      Am glad that you found this small tutorial useful . Thanks for stopping by and you too have a great week ahead !

    • Pramod Author on September 24, 2013

      Hi Obasi !
      Am glad that you found this small tutorial useful . Thanks for stopping by and you too have a great week ahead !

Have something to say?

*
CommentLuv badge