我有一个类,其中有一个方法可以在字符串中包含 NUL 字符的情况下终止程序。具体表达是这样的:
stringVar.indexOf('\u0000') < 0
这个字符串是通过 Scanner 从用户那里读取的,我真的很想知道 NUL 字符是如何通过用户的输入在字符串中结束的。
编辑:Scanner读取代码如下:
Scanner scannerVar = new Scanner(System.in);
stringVar = scannerVar.next();
最佳答案
公共(public) 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 ));
}
}
这会产生以下结果:
Found Index :-1
Found Index :-1
资源链接:
关于java - 空字符 (\u0000) 如何在字符串中结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943201/