Compare two Strings of names

edited November 2013 in How To...

Hi !

It's quiet difficult to explain but what i want to do is to compare a string with a random text (Strings text) inside and a list of surnames (Strings surnames). When that's done, i'm trying to print all the match with the surname with an other array contains some surnames and lastnames (Strings fullnames).

Might be better with an example :

Strings text = "The surname is William";

Strings [] surnames = {"John","David","William","James"};

Strings [] fullnames = {"John number1","David number1","David number1","William number1","William number2","William number3","James number1"};

and what i want i want to print is =

William number1
William number2
William number3

Don't know if you understand but if you have a piece of answer im glad to hear it !!!

Thanks a lot !

RM

PS : sorry for my english ...

Answers

  • Hi, have a look at this thread, it's similar to your question. Maybe u can try something and get back for more help if needed. : )

    http://forum.processing.org/two/discussion/1106/calculate-specific-word-frequency-in-text#Item_3

  • Actually there are two posts, and this is the first one, go here first : )

    http://forum.processing.org/two/discussion/1236/how-to-use-arrays-to-reduce-redundancy

  • edited November 2013

    Would this be what your looking for?

    String name = "William";
    String[] fullnames = {"John number1","David number1","David number1","William number1","William number2","William number3","James number1"};
    
    void setup() {
      for(int i = 0; i < fullnames.length; i++) {
        if(fullnames[i].indexOf(name) >= 0) {
          println(fullnames[i]);
        }
      }
    }
    

    -- MenteCode

  • Just in case you're wondering, you don't really need a separate array of surnames, along with an array of full names for it to search for a surname.

  • Thanks for both reply !

    @MenteCode the indexOf method works perfectly but i wonder if its possible to choses a result in the fullnames array, like only print the second result ?

  • Unless this is really what you're looking for, then you would have to be a bit more specific.

    But choosing a second (or any other number in order) is a very easy implementation:

    String name = "William";
    String[] fullnames = {
      "John number1", "David number1", "David number1", "William number1", "William number2", "William number3", "James number1"
    };
    int results = 0; //The positioning variable
    
    void setup() {
      for (int i = 0; i < fullnames.length; i++) {
        if (fullnames[i].indexOf(name) >= 0) {
          results++; //Determines the name's position in the order of surnames
          if (results == 2) { //Checks for the second name
            println(fullnames[i]);
          }
        }
      }
    }
    

    -- MenteCode

Sign In or Register to comment.