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 8846207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:55:29+00:00 2026-06-14T11:55:29+00:00

For example, I have input String: qwerty1qwerty2 ; As Output I would like have

  • 0

For example, I have input String: “qwerty1qwerty2“;

As Output I would like have [1,2].

My current implementation below:

import java.util.ArrayList;
import java.util.List;

public class Test1 {

    public static void main(String[] args) {
        String inputString = args[0];
        String digitStr = "";
        List<Integer> digits = new ArrayList<Integer>();

        for (int i = 0; i < inputString.length(); i++) {
            if (Character.isDigit(inputString.charAt(i))) {
                digitStr += inputString.charAt(i);
            } else {
                if (!digitStr.isEmpty()) {
                    digits.add(Integer.parseInt(digitStr));
                    digitStr = "";
                }
            }
        }
        if (!digitStr.isEmpty()) {
            digits.add(Integer.parseInt(digitStr));
            digitStr = "";
        }

        for (Integer i : digits) {
            System.out.println(i);
        }
    }
}

But after double check I dislake couple points:

  1. Some lines of code repeat twice.

  2. I use List. I think it is not very good idea, better using array.

So, What do you think?

Could you please provide any advice?

  • 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-14T11:55:30+00:00Added an answer on June 14, 2026 at 11:55 am

    Use replaceAll:

    String str = "qwerty1qwerty2";      
    str = str.replaceAll("[^0-9]+", " ");
    System.out.println(Arrays.asList(str.trim().split(" ")));
    

    Output:

    [1, 2]
    

    [EDIT]

    If you want to include - a.e minus, add -?:

    String str = "qwerty-1qwerty-2 455 f0gfg 4";      
    str = str.replaceAll("[^-?0-9]+", " "); 
    System.out.println(Arrays.asList(str.trim().split(" ")));
    

    Output:

    [-1, -2, 455, 0, 4]
    

    Description

    [^-?0-9]+
    
    • + Between one and unlimited times, as many times as possible, giving back as needed
    • -? One of the characters “-?”
    • 0-9 A character in the range between “0” and “9”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to learn about HTML forms. For example, I have 2 input
I have an input string which is a directory address: Example: ProgramFiles/Micro/Telephone And I
For example I have a css style like this : input.validation-passed {border: 1px solid
You have a user input string from a textbox for example var strInput =
I have an input string that will look something like Monday 2:00 pm and
I have an input string that's generated as in following example: string.Format(Document {0}, was
For example i have a string input int,can i declare a variable base on
I have two types of input string : string in a line: example: $str=
I have a string of numbers that i would like to make shorter for
I have test input string (key=value&key=value...) and key (like this D2335A9CA4924D9F914D2B47B450D436) I need to

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.