Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:13:54+00:00 2026-06-13T23:13:54+00:00

I need to check if the line contains strings that must be eliminated and

  • 0

I need to check if the line contains strings that must be
eliminated and indicate which symbols would be eliminated.
A character sequence is replaced by underscores (““), accordingly with the sequence length, if there are three or more contiguous characters with the same symbol. for example, the line “, _, @, @, @, @, $, $, , #, #,!” would be transformed into “, _, _, _, _, _, _, $, $, _, #, #,!” After the process of elimination.
I need to do this only with String or StringBuilder, Regex, ect… (Only Basic coding of Java).
Can’t use arrays also.
Thanks in advance.
This is what i tried:

public static void main(String[] args) {    
    String linha = "##,$$$$,%%%%,@%@@@,!!!!", validos = "$#%!@";        
        for (int i = 0; i < validos.length(); i++) {
            linha = linha.replaceAll("\\" + validos.charAt(i) + "{3,}", "_");
        }
        System.out.println (linha);
    }
}

The problem here is that replaces a sequence with just one “_”, and i don’t know which chars are replaced.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:13:54+00:00Added an answer on June 13, 2026 at 11:13 pm

    Surely you can do this in many ways, and probably this is a good exercise to do by yourself. Here you have a basic implementation using just basic loop structures and nothing fancy like StringUtils libraries… Note that your previous loop implementation would have missed several occurrences of the same character repeated in different locations of linha.

    static int index(String lookInStr, char lookUpChr) {
        return lookInStr.indexOf(new String(new char[] { lookUpChr, lookUpChr, lookUpChr }));
    }
    
    public static void main(String[] args) {
        String linha = "####,@@@@@@@@,$$$$,%%%%,@%@@@,!!!!", validos = "$#%!@";
        for (int i = 0; i < validos.length(); i++) {
            char currentSearchChar = validos.charAt(i);
            do {
                int index = index(linha, currentSearchChar);
                if (index >= 0) {
                    int count = -1;
                    do {
                        count++;
                    } while (linha.charAt(count + index) == currentSearchChar && count + index < linha.length() - 1);
                    String replacementSeq = "";
                    for (int j = 0; j < count; j++) {
                        replacementSeq += "-";
                    }
                    linha = linha.replaceAll("\\" + validos.charAt(i) + "{" + count + ",}", replacementSeq);
                }
            } while (index(linha, currentSearchChar) >= 0);
        }
        System.out.println(linha);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to take a variable that contains a URL, and check to see
Since I write a command line program to check cpp files, and it need
I need to check if my address has any words that matches word in
I have a varchar column which contains date in following format dd/mm/yyyy I need
I am trying to check whether a directory entered through the command line contains
I have a file called new.js.erb that contains this line: if( $('#user_password').val().length < 6
I have a CSV that contains fields that need to go to several Salesforce
I need to check for a condition on each page I visit on the
I need to check for duplicates before saving to the database in the create
I need to check some settings for all users on Windows clients in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.