我有一个类,其中有一个方法可以终止程序,以防字符串中包含 NUL 字符。具体的表达是这样的:
stringVar.indexOf('\u0000') < 0
这个字符串是通过 Scanner 从用户那里读取的,我真的很想知道 NUL 字符如何通过用户的输入最终出现在字符串中。
编辑:扫描仪读取代码如下:
Scanner scannerVar = new Scanner(System.in);
stringVar = scannerVar.next();
我有一个类,其中有一个方法可以终止程序,以防字符串中包含 NUL 字符。具体的表达是这样的:
stringVar.indexOf('\u0000') < 0
这个字符串是通过 Scanner 从用户那里读取的,我真的很想知道 NUL 字符如何通过用户的输入最终出现在字符串中。
编辑:扫描仪读取代码如下:
Scanner scannerVar = new Scanner(System.in);
stringVar = scannerVar.next();
公共 int indexOf(int ch):
返回此字符串中第一次出现指定字符的索引,如果该字符没有出现,则返回 -1。
您可以使用扫描仪获得空白输入。链接在这里:https ://stackoverflow.com/a/17504384/2293534
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to Tutorialspoint.com");
String SubStr1 = new String("Tutorials");
String SubStr2 = new String("Sutorials");
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( SubStr2 ));
}
}
这会产生以下结果:
发现指数:-1
发现指数:-1