如何在java中查找用户输入的字符串是否为基本字符串文字?

时间:2015-02-09 12:54:01

标签: java string

这里我基于名为String baseString = "nullpointer"的字符串;如果用户输入了一些字符串,例如pointernullllpointerinternullpo,那么它应该打印为TRUE,否则为FALSE。如何为此编写逻辑?请帮帮我。

public class literalString {
public static void main(String[] args) {
    String baseString = "nullpointer";
    Scanner sc = new Scanner(System.in);
    String input = sc.next();
    //pointernull, llpointer, internullpo  then return TRUE.
    // pointerllnu, unpointerll  then return FALSE
    //what shoul be the logic here?
 }
}

我们将不胜感激。

3 个答案:

答案 0 :(得分:3)

首先检查baseString.length==input.length是否失败然后是错误的 如果确实如此,请使用concatcontains

进行检查

简单示例

String baseString = "nullpointer";
    Scanner sc = new Scanner(System.in);
    String input = sc.next();
    if(baseString.length()!=input.length()){
        System.out.println("false");
    }
    else{
        if(baseString.concat(baseString).contains(input)){
            System.out.println("true");
        }
        else{
            System.out.println("false");
        }
    }

答案 1 :(得分:0)

尝试这样的事情:

String[] valueList = {"pointernull", "llpointer", "internullpo"};

if(Arrays.asList(valueList).indexOf(input) != -1) {
    // User has typed "pointernull", "llpointer" or "internullpo"
}else{
    // User has typed something else
}

答案 2 :(得分:0)

简单地:

(base.concat(base)).contains(input);

因此,如果您的基数为nullpointer,则base.concat(base)将产生nullpointernullpointer,而contains将在true上返回nullp,{{ 1}},pointernull