alexanders916 alexanders916 - 5 months ago 11x
Java Question

Search for CharSequences between two CharSeqeunces (Java)

first of all I want to say, that I'm a noobie in Java. (Sorry if the answer is that easy).
I have a String, which contains following text:

String htmlContent = "DienstagfälltMittwochfällt";

Now I have 3 CharSequences:

CharSequence dt = "Dienstag";
CharSequence mt = "Mittwoch";
CharSequence keyword = "fällt";

I want search


between the other 2 CharSequences. I shouldn't find


after the CharSequence


Is there a method or something else? And when not, how I can realize my idea?
An example wouldn't be bad :).

My code now:


public class TextScanner {

public static void main (String[] args) throws Exception{
URL url = new URL("");
InputStream is = (InputStream) url.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = br.readLine()) != null){
String htmlContent = sb.toString();
CharSequence dt="Dienstag";
CharSequence mt="Mittwoch";
CharSequence keyword="fällt";
boolean found = htmlContent.contains(keyword);
System.out.println("Es gibt eine Fehlstunde!");
System.out.println("Es gibt keine Fehlstunde!");


I want that the answer is also false, if


is after


in the string:

String htmlContent2 = "DienstagMittwochfällt";


You could use String.indexOf(String substr) to get the position of the two delimiters in the String, then use String.substring(int beginIndex, int endIndex) to get what's between them.

Note that String.indexOf(String substr) returns the starting position of the substring, so to compute the beginIndex you'll need to add the first delimiter's length.
Also, those methods work on Strings rather than CharSequence, so you'll have to convert them with .toString().

Once you've retrieved that substring, use .contains(CharSequence substr) as you currently do to check if it contains the searched CharSequence.

You can see it in action here.