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

  • Home
  • SEARCH
  • 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 9187127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:41:45+00:00 2026-06-17T19:41:45+00:00

If I have two strings .. say string1=Hello dear c’Lint and dear Bob and

  • 0

If I have two strings ..
say

string1="Hello dear c'Lint and dear Bob"

and

string2="dear"

I want to Compare the strings and delete the first occurrence of matching substring ..
the result of the above string pairs is:

Hello c'Lint and dear Bob

This is the code I have written which takes input and returns the matching occurence:

System.out.println("Enter your regex: ");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

String RegEx = bufferRead.readLine();
Pattern pattern = Pattern.compile(RegEx);
System.out.println("Enter input string to search: ");
bufferRead = new BufferedReader(new InputStreamReader(System.in));
Matcher matcher = pattern.matcher(bufferRead.readLine());

boolean found = false;
while (matcher.find()) {
    System.out.println("I found the text:\"" + matcher.group() +
            "\" starting at index \'" +
            matcher.start() + 
            "\' and ending at index \'" + 
            matcher.end() + 
            "\'");
}
  • 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-17T19:41:46+00:00Added an answer on June 17, 2026 at 7:41 pm

    You could either use:

    string result = string1.replaceFirst(Pattern.quote(string2), "");
    

    Or you could avoid regexes entirely:

    int index = string1.indexOf(string2);
    if (index == -1)
    {
        // Not found. What do you want to do?
    }
    else
    {
        String result = string1.substring(0, index) + 
                        string1.substring(index + string2.length());
    }
    

    You can report the region here using index and string2.length() very easily. Of course if you want to be able to match regular expression patterns, you should use them.

    EDIT: As noted in another answer, both of these will remove "dear" from "and_dear_Bob" leaving "and__Bob" – with the underscores representing spaces. So you’ll end up with two spaces between words. And it doesn’t force the match to be a whole word, either. It does exactly what you described, but it doesn’t give you the result you apparently want.

    Edit:
    First choice of code outputs: Hello c'Lint and dear Bob
    where Hello and c’Lint have two whitespace character in the middle.
    While this code:

    string result = string1.replaceFirst(Pattern.quote(string2+" "), ""));
    

    gets rid of additional whitespace character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two strings .. say string1=Hello Dear c'Lint and string2=Dear .. I
Let's say I have two strings: a and b. To compare whether a and
Lets say I have two strings: string s1 = hello; string s2 = hello
Let's say I have two strings: hello love The size of the maximum subarray
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Let's say I have two strings: one is XML data and the other is
Let's say I have two strings like this: XABY XBAY A simple regex that
Lets say that I have two strings: The System is in halt state. The
Let's say I have two same strings inside an ArrayList... is there a way
I have two strings in a java program, which I want to mix in

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.