-3

So I have this function:

public void actionPerformed(ActionEvent e)
{
    String input = jt.toString();
}

And I want to use substring or any other function to divide the words in this sentence.

Like using substring from 0 until it finds a "space" and then it should stop and then start again until the end of the sentence.

2
  • 3
    What was the 'something' that you tried? Might be helpful...
    – StephenTG
    Jul 25, 2013 at 15:30
  • 3
    Start looking into String#split(String) Jul 25, 2013 at 15:30

3 Answers 3

0

What you need is to use the Split method for the String class.

String[] input = jt.toString().split("\\s+");

This method will give you an array where each cell of the array will contain a word.

jt stands for JText?

If yes, you should get the text from the component instead of converting the object to string using the following instruction: jt.getText()

0
0

string[] words = input.Split(' ')

0

You can also do it with stringbuilder if you care about saving memory.

public class splitword {

  public static void main(String[] args) {

  String input = "Hi! Can you please split me into pieces :0";
  String[] toSplit = new StringBuilder(input).toString().split("[\\s\\p{P}&& [^']]+");

    for (String x : toSplit) {
        System.out.println(x);
    }

    }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.