Java 如何替换角色的某个实例
当我想替换某个字符时,我不知道如何替换它,它只替换字符串中的所有实例。我试过的Java 如何替换角色的某个实例,java,replace,Java,Replace,当我想替换某个字符时,我不知道如何替换它,它只替换字符串中的所有实例。我试过的 String s = "thaba" //I want to replace h with a and vice versa along with b and a for (int i; i < s.length; i++) if (s.substring(i, i+1).equalsIngoreCase("a")) { s = s.replace(s.substring(i, i + 1), s.substr
String s = "thaba" //I want to replace h with a and vice versa along with b and a
for (int i; i < s.length; i++)
if (s.substring(i, i+1).equalsIngoreCase("a")) {
s = s.replace(s.substring(i, i + 1), s.substring(i - 1, i));
s = s.replace(s.substring(i - 1, i), s.substring(i, i + 1));
}
尽管我希望它能有这样的结果:
tahab
或者如果我输入
thaflar
我想让它输出
tahfalr
我知道为什么会发生这种情况,但我不知道如何让它只替换那些实例
感谢您的帮助
修复
我的意思不同,我在上面修正了这个问题,它的措辞有点不正确而不是
字符串s=“tha”
s、 替换(“ha”、“ah”);这是一种方法
s=s。替换(h,a);将导致taa
因此,当你试图用h替换所有的a时,你会得到thh
您需要同时输入两个字符。在设置“h”值时,可以使用占位符字母(如x)来保留“a”值。然后将“a”值设置为x所在的位置。我将编写一个方法,并使用和替换每个字符的第一个匹配项,如
public static String replaceFirst(String in, char a, char b) {
int pos1 = in.indexOf(a);
int pos2 = in.indexOf(b);
if (pos2 < pos1) {
int temp = pos2;
pos2 = pos1;
pos1 = temp;
}
StringBuilder sb = new StringBuilder();
if (pos1 != -1 && pos2 != -1 && pos1 != pos2) {
sb.append(in.substring(0, pos1));
sb.append(b);
sb.append(in.substring(pos1 + 1, pos2));
sb.append(a);
sb.append(in.substring(pos2 + 1));
} else {
sb.append(in);
}
return sb.toString();
}
public static void main(String[] args) {
String s = "tha";
System.out.println(replaceFirst(s, 'h', 'a'));
}
输出是请求的
tah
编辑
为了支持您请求的其他功能,您还必须跟踪起始位置。首先,我们可以像这样将以前的方法委托给新方法-
public static String replaceFirst(String in, char a, char b) {
return replaceFirst(in, 0, a, b);
}
然后,我们只需调用indexOf(char,int)
来指定最小索引-
public static String replaceFirst(String in, int start, char a, char b) {
int pos1 = in.indexOf(a, start);
int pos2 = in.indexOf(b, start);
StringBuilder sb = new StringBuilder();
if (pos1 != -1 && pos2 != -1 && pos1 != pos2) {
if (pos2 < pos1) {
int temp = pos2;
pos2 = pos1;
pos1 = temp;
}
sb.append(in.substring(0, pos1));
sb.append(b);
sb.append(in.substring(pos1 + 1, pos2));
sb.append(a);
sb.append(in.substring(pos2 + 1));
} else {
sb.append(in);
}
return sb.toString();
}
哪个输出
faba
您可以存储所有索引,然后逐个替换
ArrayList<Integer> aIndexes = new ArrayList<>();
ArrayList<Integer> hIndexes = new ArrayList<>();
// find all h's and a's
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='a')
aIndexes.add(i);
if(s.charAt(i)=='h')
hIndexes.add(i);
}
// replace all a's with h's and vice versa
for(Integer i : aIndexes)
s = s.substring(0,i)+'h'+s.substring(i+1);
for(Integer i : hIndexes)
s = s.substring(0,i)+'a'+s.substring(i+1);
ArrayList<Integer> aIndexes = new ArrayList<>();
ArrayList<Integer> hIndexes = new ArrayList<>();
// find all h's and a's
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='a')
aIndexes.add(i);
if(s.charAt(i)=='h')
hIndexes.add(i);
}
// replace all a's with h's and vice versa
for(Integer i : aIndexes)
s = s.substring(0,i)+'h'+s.substring(i+1);
for(Integer i : hIndexes)
s = s.substring(0,i)+'a'+s.substring(i+1);
ArrayList aIndexes=new ArrayList();
ArrayList hIndexes=新的ArrayList();
//找到所有的h和a
对于(int i=0;i,您可以存储所有索引,然后逐个替换它们
ArrayList<Integer> aIndexes = new ArrayList<>();
ArrayList<Integer> hIndexes = new ArrayList<>();
// find all h's and a's
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='a')
aIndexes.add(i);
if(s.charAt(i)=='h')
hIndexes.add(i);
}
// replace all a's with h's and vice versa
for(Integer i : aIndexes)
s = s.substring(0,i)+'h'+s.substring(i+1);
for(Integer i : hIndexes)
s = s.substring(0,i)+'a'+s.substring(i+1);
ArrayList<Integer> aIndexes = new ArrayList<>();
ArrayList<Integer> hIndexes = new ArrayList<>();
// find all h's and a's
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='a')
aIndexes.add(i);
if(s.charAt(i)=='h')
hIndexes.add(i);
}
// replace all a's with h's and vice versa
for(Integer i : aIndexes)
s = s.substring(0,i)+'h'+s.substring(i+1);
for(Integer i : hIndexes)
s = s.substring(0,i)+'a'+s.substring(i+1);
ArrayList aIndexes=new ArrayList();
ArrayList hIndexes=新的ArrayList();
//找到所有的h和a
对于ItI= 0;IrEX是答案。你的其他输入是什么?使用一个临时占位符值。如果你想要一个通用的解决方案,你需要提供更多的示例输入。这是一个评论而不是一个答案。考虑用一些示例代码编辑你的答案,以更清楚地演示你的解决方案。当输入改变到什么时候呢?g更复杂?当输入更改为更复杂的内容时,我建议使用正则表达式。它们利用string.replace()方法,让您添加各种参数,包括何时选择项和何时不选择项。您应该将这些参数添加到您的答案中。:)如果我想在某些情况下,而不是在其他情况下,比如波依特拉变成了保罗塔尔,法巴变成了阿法布,我会怎么做?@PsyCode这是从左边开始的第一个。法巴到阿法布是两个不同的字符交换。但是你怎么用前面的字母交换所有的a,就像我说的法巴到阿法布一样,所有的a都是用上一个字母交换的这是一封很糟糕的信,我该怎么做呢?