This method takes the given text, replaces all '_' in it by ' ', and tokenizes the text. - Java java.lang

Java examples for java.lang:String Split

Description

This method takes the given text, replaces all '_' in it by ' ', and tokenizes the text.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String text = "java2s.com";
        System.out.println(java.util.Arrays.toString(tokenize(text)));
    }/*www.j  a  va 2  s . c o  m*/

    private static final String SPLIT_REGEX = "(\\W|\\s)";

    /**
     * This method takes the given text, replaces all '_' in it by ' ', and tokenizes the text.
     *
     * @param text Text to be tokenized.
     * @return tokens An array of tokens.
     *
     */
    public static String[] tokenize(String text) {

        return text.replace('_', ' ').split(SPLIT_REGEX);

    }
}

Related Tutorials