Search code examples
textfieldprocessingcontrol-p5

controlP5 textfield contents. Processing


I have a sketch in processing I am working on which contains a textfield and a submit button. When the submit button is pressed, a file with is created using the name given in the textfield. I want to make sure something has been entered into the textfield when the submit button is pressed, however, it appears that by default the string is not empty or contain white space and is not caught by if statements.

Is there any simple way to check that something has been entered in the text field without needing to resort to something like regex?


Solution

  • I am not sure I understood whether by default your string is not empty and also does not contain white space (which would make it an odd example). The best possible check I can think of is to trim whatever the entered string is and then check if it is empty:

    if(enteredString.trim().length() > 0) println("The string is valid");
    

    the trim() method trims leading and trailing spaces, so if there are only spaces they will be removed making the string empty. Also, since you are saving files you might want to check for invalid characters. With Processing (Java) you don't necessarily have to resort to regex since you can do stuff like these:

    String s = "ashd/ah";
    println(s.contains("/"));
    println(s.replace("/","-"));
    

    which will print:

    true
    ashd-ah