2014-09-29 164 views
0

我不知道如何替换某个字符,当我想替换它时,它只是替换字符串中的所有实例。我已经试过如何替换某个字符的某个实例

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)); 
} 

的问题,这是它出来与

thhbb 

尽管我希望它拿出这样的:如果

tahab 

或者我输入我想让它输出

tahfalr 

我知道这是为什么发生,但我不知道如何使它只更换这些实例 任何帮助表示赞赏。

FIX

我的意思的问题不同,我在上面固定,这是有点措辞不当

+0

正则表达式是答案吗?你的其他投入是什么? – 2014-09-29 00:37:28

+1

使用临时占位符值。 – 2014-09-29 00:38:56

+1

如果你想要一个通用的解决方案,你需要提供更多的示例输入。 – qbit 2014-09-29 00:47:14

回答

1

我会写的方法和使用String.indexOf()String.substring()更换每个字符的第一次出现,像

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(); 
} 

最后,我们可以用

叫它
public static void main(String[] args) { 
    String s = "afab"; 
    s = replaceFirst(s, 'a', 'f'); 
    s = replaceFirst(s, 2, 'a', 'b'); 
    System.out.println(s); 
} 

,输出

faba 
+0

如果我想要在某些情况下而不是其他情况下执行该操作,如poaytra变为paoytar而faba变为afab,我该怎么办? – PsyCode 2014-09-29 01:04:51

+0

@PsyCode这是从左边的第一个。 faba到afab是两种不同的角色互换。 – 2014-09-29 01:06:01

+0

但是,你如何将所有的a与前面的字母进行交换,就像我说过faba到afab时一样,所有的a都与前面的字母交换。我会怎么做呢? – PsyCode 2014-09-29 01:09:19

-1

哦,这个人是很容易,它只是replaceFirst(字符串的正则表达式, 字符串替换)meathod

1

代替

的String = “塔”

s.replace(“ha”,“ah”);是一种方法来做到这一点。

s = s.replace(h,a);将导致taa

所以当你试图用h替换所有的a时,你将得到thh。

你需要同时做两个字符。

+0

什么时候输入变成更复杂的东西? – voidHead 2014-09-29 00:55:59

+0

当输入变为更复杂的东西时,我会建议使用正则表达式。他们利用string.replace()方法的优势,让您添加各种关于何时选择项目和不选择项目的参数。 – Sevman 2014-09-29 01:21:31

+0

你应该添加到你的答案。 :) – voidHead 2014-09-29 01:35:06

0

您可以使用占位符字母(如x)在设置“h”值时保留“a”值。然后将“a”值设置为x所在的位置。

+0

这是比评论更多的回答。考虑用一些示例代码编辑您的答案,以更清楚地展示您的解决方案。 – voidHead 2014-09-29 00:52:01

0

你可以存储所有索引,然后更换逐一..

 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); 
0

你可以存储所有索引,然后更换逐一..

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); 
相关问题