I'm trying to find and read a string from a text file using a ".equal" in java. I know that I can simply do this by using ".contain" instead, but I just want to know if there is anyway to find and read the line like this:
For example I have the following data in the text file:
ID1 123 123
ID2 125 136
Now
String ID="ID1";
while ((ss = buffered_reader.readLine()) != null){
if (ss.equals(ID+[anything])){do something} }
Why I want to this is to prevent getting result for entering just "ID". So is there any way like a regex or something that can help ?
Clarification :
I want to find the exact match for the ID , for example :
if you enter ID1 then the program says : true but false for entering just ID
if I use .contain the program finds the word ID1 or ID2 and because they contain "ID" , then it always returns a true value.
now I want to use .eqaual to find the exact match followed by anything
Try this:
String ID="ID1";
while ((ss = buffered_reader.readLine()) != null) {
if (ss.matches(ID+".*")){do something} }
}
The expresion .* means any sequence of characters, even an empty sequence. So if ID is "ID1" then it will match any string that begins with ID1.
Try this regex:
ID.+
This will only match if the string has "ID" substring plus one character at least.
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