我正在读一个字符串
Is Mississippi a State where there are many systems.
我想用“t”或“T”替换每个单词中的第一个“s”或“S”(即保持相同的情况)......以便输出为:
It Mitsissippi a Ttate where there are many tystems.
我试过了
s= s.replaceFirst("(?i)S", "t"); [which of course didn't work]
并尝试使用a来分割字符串
字符串[] .split(Pattern.quote("\\s"))
然后尝试找出如何replaceFirst()
array
的每个元素,然后将值返回到string
[ 但无法找到正确的方法]。
我认为\\G
可能有助于重新启动下一个字,但却没有。任何使用这3种方法的帮助都表示赞赏。
答案 0 :(得分:10)
一种选择是将字符串拆分为单词,然后在每个单词上使用String.replaceFirst()
将s
的第一次出现替换为t
(或您想要的任何其他字母) :
<强>更新强>
我重构了我的解决方案,以找到任何s
(大写或小写)的第一次出现,并对其应用适当的转换。
String input = "Is Mississippi a State where there are many systems.";
String[] parts = input.split(" ");
StringBuilder sb = new StringBuilder("");
for (int i=0; i < parts.length; ++i) {
if (i > 0) {
sb.append(" ");
}
int index = parts[i].toLowerCase().indexOf('s');
if (index >= 0 && parts[i].charAt(index) == 's') {
sb.append(parts[i].replaceFirst("s", "t"));
}
else {
sb.append(parts[i].replaceFirst("S", "T"));
}
}
System.out.println(sb.toString());
<强>输出:强>
It Mitsissippi a Ttate where there are many tystems.
答案 1 :(得分:4)
方法-1:不使用replace
和split
方法以获得更好的效果。
String str = "Is Mississippi a State where there are many systems.";
System.out.println(str);
char[] cArray = str.toCharArray();
boolean isFirstS = true;
for (int i = 0; i < cArray.length; i++) {
if ((cArray[i] == 's' || cArray[i] == 'S') && isFirstS) {
cArray[i] = (cArray[i] == 's' ? 't' : 'T');
isFirstS = false;
} else if (Character.isWhitespace(cArray[i])) {
isFirstS = true;
}
}
str = new String(cArray);
System.out.println(str);
编辑:方法2:由于您需要使用replaceFirst
方法,而您不想使用StringBuilder
,这里有一个选项:
String input = "Is Mississippi a State where there are many Systems.";
String[] parts = input.split(" ");
String output = "";
for (int i = 0; i < parts.length; ++i) {
int smallSIndx = parts[i].indexOf("s");
int capSIndx = parts[i].indexOf("S");
if (smallSIndx != -1 && (capSIndx == -1 || smallSIndx < capSIndx))
output += parts[i].replaceFirst("s", "t") + " ";
else
output += parts[i].replaceFirst("S", "T") + " ";
}
System.out.println(output); //It Mitsissippi a Ttate where there are many Tystems.
注意: 我更喜欢方法1 ,因为方法replaceFisrt
的无开销和split
,字符串append
或concat
答案 2 :(得分:3)
使用以下修正Tim Biegeleisen的答案(在编辑他的帖子之前)
String input = "Is Mississippi a State where there are many systems.";
String[] parts = input.split(" ");
StringBuilder sb = new StringBuilder("");
for (String part : parts) {
sb.append(part.replaceFirst("s", "t").replaceFirst("S", "T"));
sb.append(" ");
}
System.out.println(sb.toString());
编辑 - 您可以使用concat()
String input = "Is Mississippi a State where there are many systems.";
String[] parts = input.split(" ");
String output = "";
for (String part : parts) {
output = output.concat(part.replaceFirst("s", "t").replaceFirst("S", "T") + " ");
}
System.out.println(output);
<强>更新强>
String input = "Is Mississippi a State where there are many Systems.";
String[] parts = input.split(" ");
//StringBuilder sb = new StringBuilder("");
String output = "";
for (String part : parts) {
output = output.concat(part.replaceFirst("s", "t") + " ");
}
String[] parts2 = output.split(" ");
output = "";
for (String part : parts2) {
output = output.concat(part.replaceFirst("S", "T") + " ");
}
System.out.println(output);
答案 3 :(得分:2)
我创建了一种方法 -
WaveShortMessage* wsm = prepareWSM("data", dataLengthBits, channel, dataPriority, -1,2);
wsm->setWsmData(blockedRoadId.c_str());
int byteLen = blockedRoadId.length(); // assuming that one char = one byte
wsm->addByteLength(byteLen);
或replace
和以下是我的代码段:
split
答案 4 :(得分:1)
我的方法将更少依赖于您提到的字符串方法。
String phrase;
String [] parts = phrase.split(" ");
for (int i = 0; i < parts.length; i++ ) {
for (int j = 0; j < parts[i].length(); j++) {
if (parts[i].charAt(j) == 's') {
parts[i] = "t" + parts[i].substring(1);
break;
} else if (parts[i].charAt(0) == 'S') {
parts[i] = "T" + parts[i].substring(1);
break;
}
}
}
String modifiedPhrase = "";
for (int i = 0; i < parts.length; i++ ) {
modifiedPhrase += parts[i] + " ";
}
答案 5 :(得分:1)
还有一个很好的,紧凑的,基于流的解决方案:
String result = Stream.of(s.split(" "))
.map(t -> t.replaceFirst("s", "t"))
.map(t -> t.replaceFirst("S", "T"))
.collect(Collectors.joining(" "));
答案 6 :(得分:0)
String ss = "Is Mississippi a State where there are many systems.";
String out = "";//replaced string
for (String s : ss.split(" ")) {
int index = s.toUpperCase().indexOf('S');
out += (s.replaceFirst("[s,S]", index!= -1 && s.charAt(index) == 'S'
? "T" : "t")) + " ";
}
System.out.println(out);