2014-09-29 165 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); 
相關問題