Monday 25 December 2017

How to spilt the input text based on hyphen using Java Regex | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=mYohn-RM9mc&list=UUhwKlOVR041tngjerWxVccw

RegexDemo.java
import java.util.regex.Pattern;

public class RegexDemo
{

    public static void main(String[] args)
    {
        String inputCharSeq = "Hello-peter-how-are-you?";

        String regex = "-";
       
        Pattern pattern = Pattern.compile(regex);

        /*
         * The split() method in the Pattern class can split a text
         * into an array of String's, using the regular expression
         * (the pattern) as delimiter.
         */

        String[] stringArrry = pattern.split(inputCharSeq);

        System.out.println("stringArrry length = " + stringArrry.length);

        for (String strValue : stringArrry)
        {
            System.out.println("strValue = " + strValue);
        }
    }

}

Output

stringArrry length = 5
strValue = Hello
strValue = peter
strValue = how
strValue = are
strValue = you?

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_spilt_hyphen.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_spilt_hyphen

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_spilt_hyphen/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment