简体   繁体   中英

Shortening the representation of a string by adding the number of consecutive characters

Given a random character string not including (0-9), I need to shorten the representation of that string by adding the number of consecutive characters. For eg: ggee will result in g2e2 being displayed.

I managed to implement the program and tested it (works correctly) through various inputs. I have run into the issue where I cannot seem to understand how the character "e" is displayed given the input above.
I have traced my code multiple times but I don't see when/how "e" is displayed when "i" is 2/3.

String input = new String("ggee");
char position = input.charAt(0);
int accumulator = 1;
for (int i = 1; i < input.length(); i++)
{
    // Correction. Was boolean lastIndexString = input.charAt(i) == (input.charAt(input.length() - 1));
    boolean lastIndexString = i == (input.length() - 1); 
    if (position == input.charAt(i))
    {
        accumulator++;
        if (lastIndexOfString)
            System.out.print(accumulator); // In my mind, I should be printing 
                                           // (input.charAt(i) + "" + accumulator); here
    }
    else //(position != input.charAt(i))
    {
        if (accumulator > 1)
        {
            System.out.print(position + "" + accumulator);
        }
        else
        {
            System.out.print(position + "");
        }
        position = input.charAt(i);
        accumulator = 1;

        if (lastIndexOfString)
           System.out.print(input.charAt(i)); // This is always printing when 
                                              //  I am at the last index of my string, 
                                              //  even ignoring my condition of 
                                              //  (position == input.charAt(i))
    }
}

In Java 9+, using regular expression to find consecutive characters, the following will do it:

static String shorten(String input) {
    return Pattern.compile("(.)\\1+").matcher(input)
            .replaceAll(r -> r.group(1) + r.group().length());
}

Test

System.out.println(shorten("ggggeecaaaaaaaaaaaa"));
System.out.println(shorten("ggggee😀😀😀😁😁😁😁"));

Output

g4e2ca12
g4e2😀6😁8

However, as you can see, that code doesn't work if input string contains Unicode characters from the supplemental planes, such as Emoji characters.

Small modification will fix that:

static String shorten(String input) {
    return Pattern.compile("(.)\\1+").matcher(input)
            .replaceAll(r -> r.group(1) + r.group().codePointCount(0, r.group().length()));
}

Or:

static String shorten(String input) {
    return Pattern.compile("(.)\\1+").matcher(input)
            .replaceAll(r -> r.group(1) + input.codePointCount(r.start(), r.end()));
}

Output

g4e2ca12
g4e2😀3😁4

Basically you want each char with no of repeats.

*******************************************************************************/

public class Main
{
    public static void main(String[] args) {
    String s="ggggggeee";
        StringBuilder s1=new 
         StringBuilder("") ;
           ;




       for(int i=0;i<s.length();i++)
      {
     int count=0,j;
        for( j=i+1;j<s.length();j++)
        {
       if(s.charAt(i)==s.charAt(j))
       count++;
       else 
         {
             break;}
        } 
        i=j-1;
     s1=s1.append(s.charAt(i)+""+(count+1));


       } 

       System.out.print(s1);



}}

Output

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM