我想使用字符串标记符忽略, " ; :
之类的特殊字符。
例如,如果我输入:
He said, "That's not a good idea."
输出应该是:
He
Said
that
s
not
a
good
idea
这是我目前的代码
class Q_03
{
public static void main(String[] argv)
{
System.out.print("Enter text to break :- ");
Scanner sc = new Scanner(System.in);
String x = sc.nextLine();
StringTokenizer url = new StringTokenizer(x, " ");
while(url.hasMoreTokens())
{
System.out.println(url.nextToken());
}
}
}
答案 0 :(得分:0)
您可以将这些特殊字符替换为regex
,如下所示:
class Q_03 {
public static void main(String[] argv) {
System.out.print("Enter text to break :- ");
Scanner sc = new Scanner(System.in);
String x = sc.nextLine().replaceAll("[|;:,'\"]", " ");
StringTokenizer url = new StringTokenizer(x, " ");
while (url.hasMoreTokens()) {
System.out.println(url.nextToken());
}
}
}
您可以将此正则表达式"[;:,'\"]"
添加到您想要的任何符号中,但某些特殊符号(例如"
)必须使用反斜杠\"
进行转义。