0

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

fällt 

between the other 2 CharSequences. I shouldn't find

fällt 

after the CharSequence

Mittwoch

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:

import java.io.*;
import java.net.*;

public class TextScanner {

   public static void main (String[] args) throws Exception{
       URL url = new URL("http://mpg-vertretungsplan.de/w/18/w00023.htm");
       InputStream is = (InputStream) url.getContent();
       BufferedReader br = new BufferedReader(new InputStreamReader(is));
       String line = null;
       StringBuffer sb = new StringBuffer();
       while((line = br.readLine()) != null){
         sb.append(line);
       }
       String htmlContent = sb.toString();
       System.out.println(htmlContent);
     CharSequence dt="Dienstag";
     CharSequence mt="Mittwoch";
     CharSequence keyword="fällt";
     boolean found = htmlContent.contains(keyword);
     if(found==true){
         System.out.println("Es gibt eine Fehlstunde!");
     }else{
         System.out.println("Es gibt keine Fehlstunde!");
     }

   }
}

I want that the answer is also false, if

keyword 

is after

mt

in the string:

String htmlContent2 = "DienstagMittwochfällt";
alexanders916
  • 157
  • 1
  • 16

3 Answers3

0

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.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • But it has to be a CharSequence, because my text hasn't any placeholders. – alexanders916 May 03 '16 at 13:51
  • What do you mean about placeholders? You can easily convert a `CharSequence` to `String` using its `.toString()` method. – Aaron May 03 '16 at 13:52
  • I meant that the String hasn't space between each CharSequence. I changed the String in the question to show it. And yes, it only should show me true or wrong. – alexanders916 May 03 '16 at 13:57
  • It doesn't matter, maybe you understood my use of "delimiters" as "space delimiters", but I meant the two specified words. Have you checked my example? You could remove every space from the data string and it would still work. – Aaron May 03 '16 at 14:01
  • @alexanders916 I've edited my example to make it more clear. Also after some thinking my method isn't so inefficient. I may add a regular expression solution if you're interested, it's another good way to solve your problem, but it requires a lot more learning if you're not familiar with regular expressions – Aaron May 03 '16 at 14:05
  • Maybe I just made a mistake once. One time I couldn't find a String because I thought that a String could be only find, when this String is seperated in his ParentString. Thank you, for explaining it! – alexanders916 May 03 '16 at 14:16
0
htmlContent.substring(htmlContent.indexOf("Mittwoch")).contains("fällt");
Andy
  • 49,085
  • 60
  • 166
  • 233
Jany Gwan
  • 77
  • 1
  • 2
  • 9
0

You can use many methods to check if the sentence is exist or not, for example:

Using Contains()

String htmlContent = "DienstagfälltMittwochfällt";
String myString = "fällt";
if(htmlContent.contains(myString)) //contains method return true or false
{
System.out.println("myString is exist")
}

another method is using indexOf

if(htmlContent.indexOf(myString)>1)//returns an Integer if it is -1 that's mean the string doesn't exist, otherwise the string is exist
{
System.out.println("myString is exist")
}
gariepy
  • 3,576
  • 6
  • 21
  • 34
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34