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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:58:13+00:00 2026-06-11T02:58:13+00:00

So I have dealt with this problem before and thought there would be an

  • 0

So I have dealt with this problem before and thought there would be an accepted pattern to solve the problem, but I have yet to find anything. I tried searching around and I have tried tinkering around myself and neither resulted in any satisfactory answers so I am turning to SO

String str = "blah1blah2"

I want to know whether the char ‘1’ or ‘2’ occurs first (this is just a made up example obviously). I know I could use str.indexOf() for 1 and 2 to compare, but this presents the problem of it possibly returning -1.

Let me know what would be a good way to tackle this.

FYI: I am working in Java but I think this sort of indexOf function is pretty common in other languages.

  • 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-11T02:58:14+00:00Added an answer on June 11, 2026 at 2:58 am

    I don’t know what degree of flexibility you require, but I would just do this the good-old-fashioned way of looping through the String, something like this:

    public static char findFirstChar(String str, char c1, char c2) {
        for (char c : str.toCharArray())
            if (c == c1 || c == c2)
                return c;
        return 0;
    }
    

    Of course, this will return the char it encounters first or 0 if neither neither chars are found in the string.

    If you want to search for an arbitrary number of character:

    public static char findFirstChar(String str, char ... chars) {
        for (char c1 : str.toCharArray())
            for (char c2 : chars)
                if (c1 == c2)
                    return c1;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've dealt with and fixed this problem for this app once before, but I
I know this is an NP-hard problem, but a lot of our users have
I have looked at the other questions related to this problem but I do
I am posting this in hopes that someone might have dealt with a similar
I have only ever dealt with database querys before in the old fashion that
I've got a wonderfully fun little SQL problem to solve today and thought I'd
I have an odd question that I have always thought about, but could never
Update I have posted my solution to this problem as an answer below. It
I want to have URLs like this: some-company-name-deal-id-6.htm So the rewrite rule should ignore
I have been struggling with this for months in my project. Here's the deal:

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.