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() +
"\'");
}
Using a Loop to remove the first occurrence of character in String. Create an empty string to store our result and a flag set to False to determine whether we have encountered the character that we want to remove. Iterate through each character in the original string.
To replace the first occurrence of a character in Java, use the replaceFirst() method.
To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.
Use the replace() method to replace the first occurrence of a character in a string. The method takes a regular expression and a replacement string as parameters and returns a new string with one or more matches replaced. Copied!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With