我希望在Java中第二次出现空格后删除文本。不应使用正则表达式。
输入:
String S1 = "VOLKSWAGEN OF SANLEANDRO";
String S2 = "Stack Overflow Site";
String S3 = "Java Learning"; -- there is only one space
输出:
VOLKSWAGEN OF
Stack Overflow
Java Learning // as there is no second space entire string should be displayed
答案 0 :(得分:2)
indexOf(" ");
以及indexOf(" ", firstSpaceIndex);
和substring(0, end)
会这样做,有关详细信息,请参阅String
API文档
答案 1 :(得分:0)
你可以尝试
String s = "Hello world good bye";
s = s.replaceFirst(" (.*?) .*", " $1");
System.out.println(s);
打印
Hello world