Can I compare a user's input with a substring?

edited October 2015 in Programming Questions

Hi there.

I'm working on an App.
Part of what it does, is print Strings of text to the screen, one by one, as the user clicks through. Most users won't be fluent in the language of the App, and I'm working on a kind of help feature. I want users to be able to type a word that they don't understand, and print the translation to the screen.

I've made a new sketch (based on this and this) to develop the mechanism:

float txtPos= random(100, 200);
String myText = "";

void setup() {
  size(800, 600);
  background(#3355cc);
}


void draw() {
  fill(#3355cc, 40);
  rect(0, 0, width, height);
  text(myText, 0, 0, width, height);
}

void keyPressed() {

  textSize(160);
  fill(#E8B600);
  text(key, txtPos, 240);
  txtPos += 70;
  if (txtPos>width) {
    txtPos-= width;
  }
  if (keyCode == BACKSPACE) {
    if (myText.length()>0) {
      myText = myText.substring(0, myText.length()-1);
    }
  } else if (keyCode == DELETE) {
    myText = "";
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT)
    myText = myText + key;

  if (keyCode == ENTER) {
    mousePressed();
  }
  print(key );
}

void mousePressed() {
  if (myText.equalsIgnoreCase("madra")) {
    println();
    print("Madra means dog!");
    myText = "";
    println();
  } else {
    println();
    print("Ní thuigim "+myText);
    myText = "";
    println();
  }
}

That works, except when ENTER is used to activate mousePressed(). I think I can fix that. But it's not a great system. I think it would be much better if the program could accept a String from the user, and check to see if any part of that string is a substring of the App's text. (as opposed to making a giant if else or switch statement block) Then I could print a more helpful answer.

Any thoughts on how to do that?
currently trying variations of
if (myText.substring(0,myText.length()-1).equalsIgnoreCase("madra")) //but that ain't it...

Answers

  • You could use the contains() function. As always, the API is your best friend.

    String text = "I have a problem";
    String word = "problem";
    println(text.contains(word)); //prints true
    println(text.contains("abc123")); //prints false
    
  • edited October 2015 Answer ✓

    Try this

    String s = "Mary had a little LAMB its fleece was white as snow";
    
    String ss = "LITTLE lamb";
    
    if(s.toLowerCase().contains(ss.toLowerCase())){
      println("Found: " + ss);
    }
    
  • edited December 2015

    Great! Thanks! will try those suggestions.

  • edited October 2015

    Thanks for your suggestions. I'm working on it right now.
    So, I think the thing to do is make an array of Strings of searchable keywords from the main text. Something like:

    String[] ss = new String[7];
    ss = {"Mary","little","lamb","fleece","was","white","snow"};
    

    and then on mousePressed() loop through ss[] looking to see if ss[i].toLowercase().contains(userInput.toLowerCase).
    Then if for example ss[4] is typed by the user, the program can print ssExplain[4] to the screen.

    Something like that?

  • Not sure if this is what you want but here's a probable solution:

    //necessary variables for textbox function
    boolean rel, rel2=true;
    
    //variable to store the textbox
    String[] input = {"", "false", ""};
    
    String[] testPhrases = {"HeLLo", "tEst", "Function", "TextBox"};
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(255);
    
      input = textbox(150,150, 100, input[0], input[1], input[2]);
    
      for (int i=0; i<testPhrases.length; i++)
        if (input[0].toLowerCase().indexOf(testPhrases[i].toLowerCase()) != -1){
          text("This is a known String", 100, 300);
        }
    }
    
    
    
    String[] textbox(int x, int y, int sx, String st, String state, String pos1){
      boolean lock=false;
      String[] str = new String[3];
      int pos=int(pos1);
      color c1,c2;
    
      str[0]=st;
    
      textFont(createFont("Lucida Sans",12));
      if (mouseX>x && mouseX<x+sx && mouseY>y && mouseY<y+20){
        c1=color(197,218,237); c2=color(87,148,191);
        if (mousePressed && mouseButton==LEFT){c1=color(181,207,231); c2=color(61,123,173); lock=true; state="true";}
      }
      else if (mousePressed && mouseButton==LEFT && !(mouseX>x && mouseX<x+sx && mouseY>y && mouseY<y+20)){
        state="false";
        c1=color(226,227,234); c2=color(171,173,179);
      }
      else {c1=color(226,227,234); c2=color(171,173,179);}
    
      if (state=="true"){
        c1=color(181,207,231); c2=color(61,123,173);
      }
    
      rectMode(CORNER);      //textbox
      stroke(c1);
      fill(255);
      rect(x,y,sx,22,2);
      stroke(c2);
      line(x+1,y,x+sx-1,y);
    
      if (state=="true" && mouseX>x && mouseX<x+sx && mouseY>y && mouseY<y+22 && mousePressed && mouseButton==LEFT){
        for (int i=str[0].length(); i>=0; i--){
          if ((i!=0 && abs(textWidth(str[0].substring(0,str[0].length()-i))-(mouseX-x-5))<=abs(textWidth(str[0].substring(0,str[0].length()-i+1))-(mouseX-x-5))) || (i==0 && str[0].length()>=1 && textWidth(str[0].substring(0,str[0].length()-1))<mouseX-x-5)){
            pos=i;
            break;
          }
        }
      }
    
      if (!keyPressed){rel=true;}
    
      textFont(createFont("Lucida Sans",12));        //cursor & position
    
      if (keyPressed && state=="true" && rel==true){
        if (pos<str[0].length() && key==CODED && keyCode==LEFT){pos+=1; rel=false;}
        if (pos>0 && key==CODED && keyCode==RIGHT){pos-=1; rel=false;}
      }
    
      if (keyPressed && state=="true"){            //writing to textbox
        if (rel==true && key!=CODED){
          if (key==BACKSPACE && str[0].length()-pos>=1){
            str[0]=str[0].substring(0,str[0].length()-pos-1)+str[0].substring(str[0].length()-pos,str[0].length());
            rel=false;
          }
          else if (key==DELETE && str[0].length()-pos<str[0].length()){
            str[0]=str[0].substring(0,str[0].length()-pos)+str[0].substring(str[0].length()-pos+1,str[0].length());
            pos-=1;
            rel=false;
          }
          else if (key!=BACKSPACE && key!=DELETE){
            if (str[0]!=null){str[0]=str[0].substring(0,str[0].length()-pos)+key+str[0].substring(str[0].length()-pos,str[0].length());}
            else {str[0]=""+key;}
            rel=false;
          }
        }
      }
    
      if (state=="true" && (millis()/100)%12<=5){
        stroke(0);
        line(x+5+textWidth(str[0].substring(0,str[0].length()-pos)),y+3,x+5+textWidth(str[0].substring(0,str[0].length()-pos)),y+18);
      }
    
      textAlign(LEFT,CENTER);
      fill(0);
      text(str[0], x+5, y+9);
    
      str[1]=state;
      str[2]=""+pos;
    
      return str;
    }
    

    NOTE: This textbox function treats the Enter key as an ordinary key and passes it to the string. If you want to use Enter as an exit (from the textbox) key let me know...

  • thank you so much! I'll consider your code carefully.

Sign In or Register to comment.