3

我需要编写一个静态方法,该方法以 aString作为参数并返回一个新方法,该方法String通过将重复相邻字母的每个实例替换为该字母的单个实例而不使用正则表达式而获得。例如,如果我输入“maaaakkee”作为 a String,它会返回“make”。我已经尝试了以下代码,但它似乎没有显示最后一个字符。这是我的代码:

import java.util.Scanner;
public class undouble {
    public static void main(String [] args){
        Scanner console = new Scanner(System.in);
        System.out.println("enter String: ");
        String str = console.nextLine();
        System.out.println(removeSpaces(str));
    }
public static String removeSpaces(String str){
    String ourString="";
    int j = 0;
    for (int i=0; i<str.length()-1 ; i++){
        j = i+1;
        if(str.charAt(i)!=str.charAt(j)){
            ourString+=str.charAt(i);
        }

    }

    return ourString;
    }
}
4

13 回答 13

42

您可以为此使用正则表达式。

例如:

String input = "ddooooonnneeeeee";
System.out.println(input.replaceAll("(.)\\1{1,}", "$1"));

输出:

done

图案说明:

  • "(.)\\1{1,}"表示任何字符(添加到第 1 组)后跟至少一次
  • "$1"参考组 1 的内容
于 2013-11-01T16:04:55.840 回答
4

也许:

for (int i=1; i<str.length() ; i++){
    j = i+1;
    if(str.charAt(i)!=str.charAt(j)){
        ourString+=str.charAt(i);
    }
}
于 2013-11-01T16:03:47.113 回答
0

问题在于你的情况。你说在每次迭代中比较 i 和 i+1 ,在最后一次迭代中你 i 和 j 都指向同一个位置,所以它永远不会打印最后一个字符。试试这个,除非你想使用正则表达式来实现这个:

编辑:

public  void removeSpaces(String str){
        String ourString="";
        for (int i=0; i<str.length()-1 ; i++){
            if(i==0){
                ourString = ""+str.charAt(i);
            }else{
                if(str.charAt(i-1) != str.charAt(i)){
                    ourString = ourString +str.charAt(i);
                }
            }           
        }
        System.out.println(ourString);
    }
于 2013-11-01T16:08:13.680 回答
0

如果您不能使用replace 或replaceAll,这里有一个替代方案。O(2n),O(N) 用于库存,O(N) 用于创建字符串。它删除字符串中所有重复的字符,将它们放入字符串生成器中。

输入:abcdef,输出:abcdef

输入:aabbcdeef,输出:cdf

private static String remove_repeated_char(String str)
{
    StringBuilder result = new StringBuilder();
    HashMap<Character, Integer> items = new HashMap<>();

    for (int i = 0; i < str.length(); i++)
    {
        Character current = str.charAt(i);
        Integer ocurrence = items.get(current);
        if (ocurrence == null)
            items.put(current, 1);
        else
            items.put(current, ocurrence + 1);
    }

    for (int i = 0; i < str.length(); i++)
    {
        Character current = str.charAt(i);
        Integer ocurrence = items.get(current);
        if (ocurrence == 1)
            result.append(current);
    }
    return result.toString();
}
于 2015-10-11T16:20:33.683 回答
0
import java.util.*;
public class string2 {

    public static void main(String[] args) {

        //removes repeat character from array
        Scanner sc=new Scanner(System.in);
        StringBuffer sf=new StringBuffer();
        System.out.println("enter a string");
        sf.append(sc.nextLine());
        System.out.println("string="+sf);
        int i=0;

        while( i<sf.length())
        {
            int j=1+i;
            while(j<sf.length())
            {   

                if(sf.charAt(i)==sf.charAt(j))
                {
                    sf.deleteCharAt(j);
                }
                else
                {
                    j=j+1;
                }
            }
            i=i+1;
        }

        System.out.println("string="+sf);
    }
}
于 2016-09-28T02:25:53.427 回答
0

输入 AABBBccDDD,输出 BD 输入 ABBCDDA,输出 C

    private String reducedString(String s){
    char[] arr = s.toCharArray();
    String newString = "";
    Map<Character,Integer> map = new HashMap<Character,Integer>();
    map.put(arr[0],1);
    for(int index=1;index<s.length();index++)
    {
        Character key = arr[index];   
        int value;
        if(map.get(key) ==null)
        {
            value =0;
        }
        else 
        {
            value = map.get(key);
        }

        value = value+1;
        map.put(key,value);
    }
    Set<Character> keyset = map.keySet();

    for(Character c: keyset)
    {
        int value = map.get(c);

        if(value%2 !=0)
        {
            newString+=c;
        }
    }

    newString = newString.equals("")?"Empty String":newString;
    return newString;
}
于 2018-02-04T00:41:47.340 回答
0
public class RemoveDuplicateCharecterInString {
    static String input = new String("abbbbbbbbbbbbbbbbccccd");
    static String output = "";
    public static void main(String[] args)
 {
        // TODO Auto-generated method stub

        for (int i = 0; i < input.length(); i++) {
            char temp = input.charAt(i);
            boolean check = false;

            for (int j = 0; j < output.length(); j++) {
                if (output.charAt(j) == input.charAt(i)) {
                    check = true;
                }
            }
            if (!check) {
                output = output + input.charAt(i);
            }
        }
        System.out.println("  " + output);
    }
}

答案:abcd

于 2018-03-27T10:06:34.980 回答
0
public class RepeatedChar {

    public static void main(String[] args) {
        String rS = "maaaakkee";
        String outCome= rS.charAt(0)+"";
        int count =0;
        char [] cA =rS.toCharArray();
        for(int i =0; i+1<cA.length; ++i) {
            if(rS.charAt(i) != rS.charAt(i+1)) {
                outCome += rS.charAt(i+1);
            }
        }

        System.out.println(outCome);
    }

}
于 2018-05-01T04:54:32.920 回答
0

编写删除重复字符的 JAVA 程序:

package replace;

public class removingrepeatedcharacters 

{

public static void main(String...args){
        int i,j=0,count=0;

        String str="noordeen";
        String str2="noordeen";
        char[] ch=str.toCharArray();
        for(i=0;i<=5;i++)
        {
            count=0;
            for(j=0;j<str2.length();j++)
            {
            if(ch[i]==str2.charAt(j))
            {
                count++;
                System.out.println("at the index "+j +"position  "+ch[i]+    "+ count is"+count);
                if(count>=2){
                    str=str2;
                    str2=str.replaceFirst(Character.toString(ch[j]),Character.toString(' '));
            }

                System.out.println("after replacing    "          +str2);   

            }

            }




        }

    }

}
于 2018-06-29T05:08:08.267 回答
0
String outstr = "";
String outstring = "";
for(int i = 0; i < str.length() - 1; i++) {
    if(str.charAt(i) != str.charAt(i + 1)) {
        outstr = outstr + str.charAt(i);
    }
    outstring = outstr + str.charAt(i);         
}
System.out.println(outstring);
于 2018-08-10T10:54:29.850 回答
0
public static void remove_duplicates(String str){
    String outstr="";
    String outstring="";
    for(int i=0;i<str.length()-1;i++) {
    if(str.charAt(i)!=str.charAt(i+1)) {
        outstr=outstr+str.charAt(i);
        }
        outstring=outstr+str.charAt(i);
        }
        System.out.println(outstring);
    }
于 2018-08-10T12:11:07.823 回答
-1

使用 java 7 更有趣:

System.out.println("11223344445555".replaceAll("(?<nums>.+)\\k<nums>+","${nums}"));

正则表达式中不再有神秘的数字。

于 2014-07-08T09:38:52.943 回答
-2

公共静态字符串removeDuplicates(字符串str){

    String str2 = "" + str.charAt(0);
    for (int i = 1; i < str.length(); i++) {
        if (str.charAt(i - 1) == str.charAt(i) && i != 0) {
            continue;
        }
        str2 = str2 + str.charAt(i);
    }
    return str2;
}
于 2016-05-01T16:07:25.697 回答