Why String.contains() can not accept a regex as a parameter? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Any idea to Implementing this?
Is this best way to do?
boolean contains=string.split(regex,2).length==2;
Thanks a lot for any suggestion.

You'd usually use something like:
boolean contains = pattern.matcher(text).find();
where pattern is an instance of java.util.regex.Pattern.

This is easily implemented in terms of Matcher.find():
public static boolean containsRegex(String input, String regex) {
return Pattern.compile(regex).matcher(input).find();
}
However, it is rarely necessary as you can simply stay with matches, slightly expanding your regex to begin and end with .*.

I would use
boolean contains = Pattern.compile(regex).matcher(string).find();

Related

How to achieve the same result as with filter and findFirst [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Is there a more elegant way to achieve by using java 8 or above the eighth version what's below?
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter(s -> s.equals("test")).findFirst();
Thanks in advance
It depends on what exactly you want to do.
If you just want to check if "test" is in one of the elements, you could just use .contains():
List.of("test","test1").contains("test");
If you want to find the first element fitting a condition, you can omit creating the list and directly create a Stream:
Stream.of("test","test1").filter(s->"test".equals(s)).findFirst()
If you want to check if an element fitting the condition exist, you can use anyMatch:
Stream.of("test","test1").anyMatch(s->"test".equals(s))
This is probably the best your going to get.
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter("test"::equals).findFirst();
If its reused you can just make a method out of the 2nd line. Question has also already been answered here

How do I find all substrings within two common characters in a given String? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What I have:
String result = "<>hello<!><>Soumik<!><>Having a wonderful day?<!>";
What I need:
resultStrings = ["hello", "Soumik", "Having a wonderful day?"];
This regex should do the trick:
<[^>]*>([^<]+)<
Find all matches, and extract capturing group 1 from each.
Regex demo
How about that:
result = result.replace("<", "");
result = result.replace(">","";
resultStrings = result.split("!");
It's really simple.
I don't know other conditions so it may not be useful. Please add conditions so I can respond to it.

Trying to pull out certain numbers from a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));

trying to obtain a first name in the path after first back slash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.

Why java has no not function/ alternatives for same [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Ok so if you want to check if a list is not empty
we would need to do something like
if(! mylist.isEmpty())
this affects code readability, so how can we write the same thing in a readable way, calling out negation of condition check.
One of possibility is to have a static helper function like:
static boolean not(boolean condition) { return !condition;}
How bad is this idea? Are there other options in apache common or guava etc? Or any other way you have achieved this?
It is not a bad idea in itself and it will not affect anything.
However I think many people will disagree with you regarding the "unreadability" of the normal way of negating things using !.
if ( ! list.isEmpty() )
versus
if ( not( list.isEmpty() ) )
does not make much of a difference IMHO.

Categories