Java 中的子字符串 - 如何断开句子并另存为不同的字符串
Substring in Java - how to break sentence and save as different strings
我想将句子的一部分(一个字符串)保存到另一个字符串中。
例如:
String s = " 到 1889 年,由于问候语和电话之间的关联,中央电话交换运营商被称为 'hello-girls' "
我想把"By 1889, central telephone exchange operators were known as"存成一个字符串
和
"hello-girls' due to the association between the greeting and the telephone"变成另一个.
如何操作?
使用下面的代码
String s = " By 1889, central telephone exchange operators were known as 'hello-girls' due to the association between the greeting and the telephone ";
int index=s.indexOf("'");
String s1=s.substring(0,index);
String s2=s.substring(index,s.length()-1);
System.out.println(s1);
System.out.println(s2);
看到这个ideone https://ideone.com/iB5quw
尝试:
int index = s.indexOf("'hello-girls'");
System.out.println(s.substring(0, index ));
System.out.println(s.substring(index));
输出:
By 1889, central telephone exchange operators were known as
'hello-girls' due to the association between the greeting and the telephone
文档:
String s = " By 1889, central telephone exchange operators " +
"were known as 'hello-girls' due to the association " +
"between the greeting and the telephone ";
第一种方式:
String[] strings = s.split("as ");
String first = strings[0] + "as";
// "By 1889, central telephone exchange operators were known as"
String second = strings[1];
// "'hello-girls' due to the association between the greeting and the telephone"
第二种方式:
String separator = " as ";
int firstLength = s.indexOf(separator) + separator.length();
String first = s.substring(0, firstLength);
// "By 1889, central telephone exchange operators were known as"
String second = s.substring(firstLength);
// "'hello-girls' due to the association between the greeting and the telephone"
如果这是因为单词 'hello-girls' 你可以这样做:
int index = s.indexOf("'hello-girls'"));
String firstPart = s.substring(0, index);
String secondPart = s.substring(index);
请注意,firstPart 字符串将以空 space 结尾。您可以通过更改上面的代码轻松删除它:
String firstPart = s.substring(0, index - 1);
我想将句子的一部分(一个字符串)保存到另一个字符串中。
例如:
String s = " 到 1889 年,由于问候语和电话之间的关联,中央电话交换运营商被称为 'hello-girls' "
我想把"By 1889, central telephone exchange operators were known as"存成一个字符串
和
"hello-girls' due to the association between the greeting and the telephone"变成另一个.
如何操作?
使用下面的代码
String s = " By 1889, central telephone exchange operators were known as 'hello-girls' due to the association between the greeting and the telephone ";
int index=s.indexOf("'");
String s1=s.substring(0,index);
String s2=s.substring(index,s.length()-1);
System.out.println(s1);
System.out.println(s2);
看到这个ideone https://ideone.com/iB5quw
尝试:
int index = s.indexOf("'hello-girls'");
System.out.println(s.substring(0, index ));
System.out.println(s.substring(index));
输出:
By 1889, central telephone exchange operators were known as
'hello-girls' due to the association between the greeting and the telephone
文档:
String s = " By 1889, central telephone exchange operators " +
"were known as 'hello-girls' due to the association " +
"between the greeting and the telephone ";
第一种方式:
String[] strings = s.split("as ");
String first = strings[0] + "as";
// "By 1889, central telephone exchange operators were known as"
String second = strings[1];
// "'hello-girls' due to the association between the greeting and the telephone"
第二种方式:
String separator = " as ";
int firstLength = s.indexOf(separator) + separator.length();
String first = s.substring(0, firstLength);
// "By 1889, central telephone exchange operators were known as"
String second = s.substring(firstLength);
// "'hello-girls' due to the association between the greeting and the telephone"
如果这是因为单词 'hello-girls' 你可以这样做:
int index = s.indexOf("'hello-girls'"));
String firstPart = s.substring(0, index);
String secondPart = s.substring(index);
请注意,firstPart 字符串将以空 space 结尾。您可以通过更改上面的代码轻松删除它:
String firstPart = s.substring(0, index - 1);