1

I have a String text "Welcome!to java@";

I know how to convert it to receive array [Welcome, to, java]

    String[] aaa = text.split("[\\p{IsPunctuation}\\p{IsWhite_Space}]");
    System.out.println(Arrays.toString(aaa));   //priinting

But I need to include punctuation. I need to receive [Welcome, !, to, java, @]

Anybody know who to receive it ?

The reason why I need to do it, because i have a randomise(char[] cw) method, which is tweaking words for me. However that method is going crazy when punctuation is included

public void randomise(char[] cw) 
    {
        for (int i = 1; i < cw.length-1; i++) 
        {
            //my range
            int range= (int)(Math.random() * (cw.length - i - 1));

            //swap index
            int index=i+range;

            //swap
            char temp  =  cw[i];
            cw[i] = cw[index]; 
            cw[index] = temp;

        }
        System.out.println(cw);

    }

Thanks for reply

Andrew_Dublin
  • 745
  • 6
  • 22
  • 1
    Does http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters or http://stackoverflow.com/questions/275768/is-there-a-way-to-split-strings-with-string-split-and-include-the-delimiters contain the answer you are looking for? Alternatively, check out [`StreamTokenizer`](http://docs.oracle.com/javase/7/docs/api/java/io/StreamTokenizer.html) with a `ByteArrayInputStream` and '!', '@', etc. set to "ordinary" characters. – Jason C Nov 06 '14 at 18:24
  • 1
    yes. your second link helped me. Googled it and coundnt find. What words did you type to find it ? Thanks a lot – Andrew_Dublin Nov 06 '14 at 18:38
  • Something like ["java split string keep delimiter"](https://www.google.com/search?q=java+split+string+keep+delimiter), with "keeping delimiters" being the key terminology there. The `StreamTokenizer` I just already had knowledge of from past experience. – Jason C Nov 06 '14 at 18:48
  • actually never typed "keep delimiter" but tried many variations of java string convert etc... . Thanks again – Andrew_Dublin Nov 06 '14 at 18:50

1 Answers1

1

Found a help thanks to JasonC

String str = "Welcome!to java@";
inputs = str.split("(?!^)\\b");
Andrew_Dublin
  • 745
  • 6
  • 22