Java字符串操作:比较Java中的相邻字符 [英] Java String Manipulation : Comparing adjacent Characters in Java

查看:54
本文介绍了Java字符串操作:比较Java中的相邻字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题
给定一个字符串,返回一个"cleaned"字符串,其中相同的相邻字符已减少为单个字符.因此,"yyzzza" 产生"yza" .

stringClean("yyzzza") → "yza"      
stringClean("abbbcdd") → "abcd"       
stringClean("Hello") → "Helo"

我正在尝试输入 stringClean("abbbcdd")→"abcd"

我的代码在下面.在进行相邻字符比较之后,我得到了部分追加的字符串,因此截至目前,我正在获取追加的stringBuilder "sb = abc" ,这不是正确的输出,我应该得到输出为"abcd"

My code is below.Im getting the partial appended string after doing the adjacent character comparison hence as of now im getting appended stringBuilder "sb=abc" which is not the correct output i should get the output as "abcd",

class cleanString{

    public static String stringClean(String str){
        int startIndex = str.indexOf(str);
        char startChar = '\u0000';
        char adjacentChar = '\u0000';
        System.out.println("startIndex-->" + startIndex);
        final StringBuilder sb = new StringBuilder();

        for(startIndex = 0; startIndex < str.length(); startIndex += 1){
            startChar = str.charAt(startIndex);
            System.out.println("startIndex ::" + startIndex);
            System.out.println("startChar ::" + startChar);

            final int adjacentPosition = startIndex + 1;
            System.out.println("adjacentPosition ::" + adjacentPosition);
            if(adjacentPosition != str.length()){
                adjacentChar = str.charAt(adjacentPosition);
                System.out.println("adjacentChar ::" + adjacentChar);
            }
            if(startChar == adjacentChar){
                System.out.println("startChar ::" + startChar);
                System.out.println("adjacentChar::" + adjacentChar);

                System.out.println("Before Substring string --->" + str);
                str = str.substring(1);
                startIndex--;
                System.out.println("After Substring string --->" + str);
                System.out.println("IndexOf check ---->"
                    + sb.toString().indexOf(startChar));
                if(sb.toString().indexOf(startChar) != -1){
                    sb.append(adjacentChar);
                    System.out.println("Appended String in if part-->"
                        + sb.toString());
                }
            } else{
                str = str.substring(1);
                startIndex--;
                sb.append(startChar);
                System.out.println("Appended String --->" + sb.toString());
            }
        }// end of for loop
        return sb.toString();
    }

    //im getting output as abc...which is partial appended string      
    public static void main(String ...args){     
        String outputCleanString=new cleanString().stringClean("abbbcdd");      
        System.out.println("Cleaned String --->"+outputCleanString);
    }      

}  

* 观察:在我获得了附加的字符串"abc"之后,然后当我比较该字符的最后一组字符"dd"时,我遇到了这个问题.

*Observation:*after i get the appended string "abc",and then when i move to compare the final set of characters "dd" im facing the problem in that part.

推荐答案

对于您的代码和特定问题,您已经提到,如果相邻位置超出了字符串的范围,则将neighborChar设置为null char,否则将neighborChar视为字符串中的最后一个字符,表示未完成附加操作.

For your code and the specific issue you have mentioned if the adjacent position is beyond the bounds of your string set adjacentChar to the null char as otherwise adjacentChar is seen as the last character in the string, which means that an append is not done.

if(adjacentPosition != str.length()){
     adjacentChar = str.charAt(adjacentPosition);
     System.out.println("adjacentChar ::" + adjacentChar);
}

else {
     adjacentChar = '/u0000';
}

编辑

我认为您提到的第二个问题是在这段代码中

I think that the second issue you have mentioned is in this piece of code

 if(sb.toString().indexOf(startChar) != -1){
      sb.append(adjacentChar);
      System.out.println("Appended String in if part-->"
         + sb.toString());
 }

由于e和o在Hello的缓冲区中,因此在检查Bookkeeper时会附加它们.我认为您不需要该行,因此请将其删除,这应该可以解决Hello Bookkeeper.

As e and o are in the buffer from Hello they are being appended when Bookkeeper is being checked. I don't think you need that line so remove it and that should fix Hello Bookkeeper.

尽管Mohoamed的答案也可以.

Although Mohoamed's answer will also work.

这篇关于Java字符串操作:比较Java中的相邻字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆