14 Replies Latest reply: Oct 25, 2007 7:57 AM by 807600 RSS

    Partial String match question

    807600
      Hi!

      I am trying to make a program that reads two Strings and check if the last String is a part of the first. The thing is, I am not allowed to use the String methods (like equals/compare etc..). I have to convert the strings to unicode and then compare. The method shall return if the second string is a part of the first, and in that case, what place in the first string is the second string starting. Example: string1: "this is a string", string2: "is a". Returned: string2 is a part of string1 and starts at: 5.

      The code I have written just check the first letter in the strings. How do I check the whole string?

      Cheers
      Terje
      import javax.swing.JOptionPane;
      
      public class oppg2c {
           public static void main (String []args) {
                //deklarere variabler
                String          innlestTekst1, innlestTekst2;
                int               plassering; 
      
                //Leser inn tekst fra tastaturet
                innlestTekst1     =     JOptionPane.showInputDialog("Skriv inn tekst 1 : ");
                innlestTekst2     =     JOptionPane.showInputDialog("Skriv inn tekst 2 : ");
      
                //kaller metoden som sjekker om tekst2 er en delstreng av tekst1
                plassering          =      delStreng(innlestTekst1, innlestTekst2);
      
                //skriver ut
                System.out.println("Tekst 1 er..: " +innlestTekst1);
                System.out.println("Tekst 2 er..: " +innlestTekst2);
                System.out.println("Plassering..: " +plassering);
           }
           //oppretter metode for sjekk av delstreng
           public static int delStreng (String text1, String text2) {
                char          txt1, txt2;
                int               uniCode1, uniCode2, plass=0, sumTxt2=0;
      
                for (int j=0; j<text2.length(); j++) {
                     txt2          =     text2.charAt(j);
                     uniCode2     =     (int)txt2;
                     for (int i=0; i<text1.length(); i++) {
                          txt1          =     text1.charAt(j);
                          uniCode1     =     (int)txt1;
                          if (uniCode2 == uniCode1) {
                               return plass = i;
                          }//end if
                     }//end for indre
                }//end for ytre
                return plass;
           }//end metode delStreng
      }//end klasse oppg2c
        • 1. Re: Partial String match question
          masijade
          the contains method of String? String implements CharSequence, and contains takes a CharSequence, so, it sounds like
          string1.contains(string2);
          is all you need. Read the API.
          • 2. Re: Partial String match question
            807600
            Thanks, but we are not "allowed" to use this method (this is an assignment). I think it`s meant to be an excersise in control structures.

            -Terje
            • 3. Re: Partial String match question
              807600
              You can see the source code of the method indexOf(String str) in String class, that do exactly that you need.

              If you don't like to see this code, I suggest you use the method codePointAt, that: "Returns the character (Unicode code point) at the specified index".
              • 4. Re: Partial String match question
                masijade
                nested loops using charAt.
                • 5. Re: Partial String match question
                  807600
                  Yeah, nested loops and charAt I have understood (see my code). It`s just that I`m hitting the wall when trying to match the whole pattern of string2.

                  -Terje
                  • 6. Re: Partial String match question
                    800308
                    oppretter metode for sjekk av delstreng
                    locates substring
                    German is verbose. My fingers are glad I speak english.
                    • 7. Re: Partial String match question
                      807600
                      hehe.. Sorry, the comments are in Norwegian.

                      -Terje
                      • 8. Re: Partial String match question
                        masijade
                        Something like this? (untested, but you should see the concept)
                        String a = "abcdefgh";
                        String b = "def";
                        int start = -1;
                        for (int i = 0; (i < (a.length() - b.length() + 1) && start < 0); i++) {
                          if (a.charAt(i) == b.charAt(0)) {
                              for (int j = 1; j < b.length(); j++) {
                                if (a.charAt(i + j) != b.charAt(j) {
                                  start = -1;
                                  break;
                                } else if (j == (b.length() -1)) {
                                    start = i;
                                }
                              }
                            }
                        }
                        • 9. Re: Partial String match question
                          807600
                          Yeah! That worked. Thank you! (even if it omit the String to Char to Int conversion).

                          -Terje
                          • 10. Re: Partial String match question
                            800308
                            Too late... but here's my tested version.
                            public class StringMatcherator
                            {
                              public static void main (String []args) {
                                String text = "Some text to search.";
                                String seek = "search";
                                System.out.println("The first occurence of \""+seek+"\" starts at "+indexOf(text, seek)+" in \""+text+"\".");
                              }
                            
                              private static int indexOf(String text, String seek) {
                                int seekLength = seek.length();
                                int last = text.length() - seekLength;
                                if(last<0) return(-1);
                                char[] t = text.toCharArray();
                                char[] s = seek.toCharArray();
                                for (int i=0; i<last; i++) {
                                  boolean found = true;
                                  for (int j=0; j<seekLength; j++) {
                                    if (s[j] != t[i+j]) {
                                      found = false;
                                      break;
                                    }
                                  }
                                  if (found) return(i);
                                }
                                return(-1);
                              }
                            
                            }
                            For efficiency, I use a couple of char arrays to avoid all those calls to charAt() and length().

                            Edited by: corlettk on Oct 25, 2007 2:01 PM
                            • 11. Re: Partial String match question
                              807600
                              Thanks! What a brilliant forum this is!

                              -Terje
                              • 12. Re: Partial String match question
                                masijade
                                corlettk wrote:
                                For efficiency, I use a couple of char arrays to avoid all those calls to charAt() and length().
                                I just didn't worry about it. ;-)

                                You liked the concept though, did you? I'm a big fan of "short-circuting" loops as quickly as possible.
                                Many people I know say you should never use break (coding standards people), but I generally deal with
                                arrays (and the like) that are 20,000+ elements long. Nest three of those and then loop all the way through,
                                it takes a little while, especially when you know after 5 iterations in the third nested loop during the 10th
                                iteration in the second loop, and the third iteration in the first loop, that you already have what you need.
                                • 13. Re: Partial String match question
                                  807600
                                  Yes, it`s a clever construct you showed me there. Which I, of course, only 3 months into a bachelor of computerscience course don`t fully understand. hehe (but it worked!). For example, why the expression: (text1.length() - text2.length() + 1) ?? (Especially the +1)

                                  --Terje                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                  • 14. Re: Partial String match question
                                    800308
                                    especially when you know after 5 iterations in the third nested loop during the 10th iteration in the second loop, and the third iteration in the first loop
                                    Yep... that and I can follow the code with the breaks... but I can't follow that sentence... it's got too much "state" for my tiny mind to hold at one time.