-4

Is there a method in Java where you can get a sub-string from a string? Like for example, I have a:

string_a = "B1 B2 B4 B3"

Is there a way to check if the string_b = "B3" exists in the string_a? I'm doing a program where string_a can change its value but string_b is constant. I need to check if string_a has a string_b.

1
  • 8
    string_A.contains(string_b) Oct 5, 2015 at 14:03

3 Answers 3

7

Check the Javadocs for answers to questions like this.

The method you want is String.contains(CharSequence). A String is a CharSequence, so you can pass in a String as the parameter.

5

The method you're looking for is contains it takes either char or string as an argument and returns true or false. So for your problem, you need:

string_a.contains(string_b)
3

Yes, it's called contains. So you would be doing:

bool result;
String stringOne = "test";
result = stringOne.contains("st"); // result is now true

Not the answer you're looking for? Browse other questions tagged or ask your own question.