要求是从String中删除三个连续的相同字符

时间:2017-12-13 15:19:50

标签: java arrays string loops

private void btnInsert_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtInsert.Text))
    {
        if (lstIntegers.Items.Contains(txtInsert.Text))
        {
            MessageBox.Show("Number already exists in list", "error", MessageBoxButtons.OK);
            txtInsert.Text = string.Empty;
            txtInsert.Focus();
            return;
        }
        else
        {
            var x = Convert.ToInt32(txtInsert.Text);
            if (Enumerable.Range(1,100).Contains(x))
            {
                lstIntegers.Items.Add(txtInsert.Text);
                txtInsert.Clear();
                txtInsert.Focus();
                bubbleSort();
            }
            else
            {
                MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
                txtInsert.Text = string.Empty;
                txtInsert.Focus();
                return;
            }
        }

    }
    else
    {
        MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
        txtInsert.Text = string.Empty;
        return;
    }

    if (lstIntegers.Items.Count == 30)
    {
        MessageBox.Show("Maximum number of entries exceeded", "error", MessageBoxButtons.OK);
        //button enabled was false however couldn't then add another 
        btnInsert.Enabled = true;
    }
}   

我的代码现在删除了两个相同的字符。例如,如果输入字符串是" abcccddefgggh"那么输出应该是" abddefh"

3 个答案:

答案 0 :(得分:2)

如果你确定删除3个连续的字符,那么直接的方法就是 -

StringBuffer sb = new StringBuffer();
 for (int i = 0; i < str.length(); i++) {
     if(i+2 < str.length() && str.charAt(i) == str.charAt(i+1) && str.charAt(i) == str.charAt(i+2)) {
          i+=2;
          continue;
     }
     sb.append(str.charAt(i));
 }
 System.out.println("The new String is "+ sb);

答案 1 :(得分:1)

一种方法是计算一个字符在决定是否输出之前重复的次数。见下文:

static String removeConsecutive(String s, int minRepeatsToRemove) {
    int pos = 0;
    StringBuilder sb = new StringBuilder();

    while (pos < s.length()) {
        char c = s.charAt(pos);
        int repeats = 1;
        while (pos + repeats < s.length() && c == s.charAt(pos + repeats))
            repeats++;

        if (repeats < minRepeatsToRemove)
            for (int i = 0; i < repeats; i++)
                sb.append(c);

        pos += repeats;
    }

    return sb.toString();
}

public static void main(String[] args) {
    System.out.println(removeConsecutive("abcccddefgggh", 3));
}

答案 2 :(得分:0)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.apache.log4j;

import java.util.Scanner;

public class Sample {

    public static void main(String ar[]) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        String str;

        String[] arr = new String[len];
        for (int i = 0; i < len; i++) {
            str = sc.next();
            arr[i] = "";
            String temp = str;
            for (int j = 0; j < (temp.length() - 3);) {
                if (temp.charAt(j) == temp.charAt(j + 1) && temp.charAt(j) == temp.charAt(j + 2)) {
                    temp = temp.substring(0, j) + "" + temp.substring(j + 3);
                    j = j + 3;
                } else {
                    arr[i] = arr[i] + temp.charAt(j);
                    j = j + 1;                    
                }
            }
            if (temp.length() > 2) {
                if (arr[i].length() != 0) {
                    if (!(str.charAt(str.length() - 3) == str.charAt(str.length() - 2)) || !(str.charAt(str.length() - 3) == str.charAt(str.length() - 1))) {
                        if (!(str.charAt(str.length() - 3) == str.charAt(str.length() - 5)) || !(str.charAt(str.length() - 3) == str.charAt(str.length() - 4))) {
                            arr[i] = arr[i] + str.charAt(str.length() - 3) + str.charAt(str.length() - 2) + str.charAt(str.length() - 1);
                        } else {
                            arr[i] = arr[i] + str.charAt(str.length() - 2) + str.charAt(str.length() - 1);
                        }
                    }
                }
            } else {
                arr[i] = temp;
            }           
            if (arr[i].length() == 0) {
                System.out.println("-1");
            } else {
                System.out.println(arr[i]);
            }
        }
    }
}