-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.

4

3 回答 3

1

您需要对 String 类使用 Split 方法。

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

此方法将为您提供一个数组,其中数组的每个单元格都将包含一个单词。

jt代表JText

如果是,您应该从组件中获取文本,而不是使用以下指令将对象转换为字符串:jt.getText()

于 2013-07-25T15:33:23.170 回答
0

如果您关心节省内存,也可以使用 stringbuilder 来完成。

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);
    }

    }
    }
于 2013-07-25T16:02:12.143 回答
0

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

于 2013-07-25T15:41:20.140 回答