Java 什么会导致String.charAt(0)不打印任何内容,并且是字符类型“0”;“16”字;?
有人知道这里会发生什么吗 第一个块显示了我通常希望看到的内容—字符串的第一个字符位于索引“0”中,注释掉了“问题”字符串,替换为完全相同的内容,但以前从未运行过Java 什么会导致String.charAt(0)不打印任何内容,并且是字符类型“0”;“16”字;?,java,string,character,comma,gettype,Java,String,Character,Comma,Gettype,有人知道这里会发生什么吗 第一个块显示了我通常希望看到的内容—字符串的第一个字符位于索引“0”中,注释掉了“问题”字符串,替换为完全相同的内容,但以前从未运行过 public void finderTest(){ String theDoc = "Hello, I want this to work, and work well! Do you think it will work, and if not, why not?"; //String wordOne = "abc"
public void finderTest(){
String theDoc = "Hello, I want this to work, and work well! Do you think it will work, and if not, why not?";
//String wordOne = "abc"; // old, pre-used string, used to hold a comma.
String wordOne = "abc";// new, never run before with a comma
String wordTwo = "and";
System.out.println("Type of character at index '0' in theDoc: "+Character.getType(theDoc.charAt(0)));
System.out.println("Character at index '0' in theDoc: "+theDoc.charAt(0));
System.out.println();
System.out.println("All of wordOne: "+"'"+wordOne+"'");
System.out.println("Type of character at index '0' in wordOne: "+Character.getType(wordOne.charAt(0)));
System.out.println("Character at index '0' in wordOne: "+wordOne.charAt(0));
System.out.println();
System.out.println("Type of Character at index '0' in wordTwo: "+Character.getType(wordTwo.charAt(0)));
System.out.println("Character at index '0' in wordTwo: "+wordTwo.charAt(0));
}
其输出:
/*
Type of character at index '0' in theDoc: 1
Character at index '0' in theDoc: H
All of wordOne: 'abc'
Type of character at index '0' in wordOne: 2 // okay
Character at index '0' in wordOne: a // okay
Type of Character at index '0' in wordTwo: 2
Character at index '0' in wordTwo: a
*/
/*
Type of character at index '0' in theDoc: 1
Character at index '0' in theDoc: H
All of wordOne: 'abc'
Type of character at index '0' in wordOne: 16 // What does this mean?
Character at index '0' in wordOne: // where is the a? (well, its in wordOne index '1'... but why??)
Type of Character at index '0' in wordTwo: 2
Character at index '0' in wordTwo: a
*/
第二个块注释掉了“new”字符串,“wordOne”的第一个字符为空。它不是空字符或换行符。我一直在使用该变量在“theDoc”中查找逗号……但当我运行它时,索引“0”中没有任何内容,而索引1中有逗号。如果复制并粘贴字符串,问题仍然存在。然而,注释掉/删除它,就可以解决这个问题
public void finderTest(){
String theDoc = "Hello, I want this to work, and work well! Do you think it will work, and if not, why not?";
String wordOne = "abc"; // now running old string, used to hold comma
//String wordOne = "abc";
String wordTwo = "and";
System.out.println("Type of character at index '0' in theDoc: "+Character.getType(theDoc.charAt(0)));
System.out.println("Character at index '0' in theDoc: "+theDoc.charAt(0));
System.out.println();
System.out.println("All of wordOne: "+"'"+wordOne+"'");
System.out.println("Type of character at index '0' in wordOne: "+Character.getType(wordOne.charAt(0)));
System.out.println("Character at index '0' in wordOne: "+wordOne.charAt(0));
System.out.println();
System.out.println("Type of Character at index '0' in wordTwo: "+Character.getType(wordTwo.charAt(0)));
System.out.println("Character at index '0' in wordTwo: "+wordTwo.charAt(0));
}
其输出:
/*
Type of character at index '0' in theDoc: 1
Character at index '0' in theDoc: H
All of wordOne: 'abc'
Type of character at index '0' in wordOne: 2 // okay
Character at index '0' in wordOne: a // okay
Type of Character at index '0' in wordTwo: 2
Character at index '0' in wordTwo: a
*/
/*
Type of character at index '0' in theDoc: 1
Character at index '0' in theDoc: H
All of wordOne: 'abc'
Type of character at index '0' in wordOne: 16 // What does this mean?
Character at index '0' in wordOne: // where is the a? (well, its in wordOne index '1'... but why??)
Type of Character at index '0' in wordTwo: 2
Character at index '0' in wordTwo: a
*/
java中的逗号或符号是否会导致这样的问题?我尝试使用字符数组,清理工作区来重建一切,但没有任何改变……这对于在句子中查找“ngram”的索引是一个巨大的问题,当一些gram类似于“and”。昨晚有一次,它在工作,然后突然开始不工作。我很困惑
有什么想法吗
谢谢
Andrew我尝试将您的示例粘贴到Eclipse中,它告诉我: 某些字符无法使用“Cp1252”字符编码进行映射 并指向字符串中的第一个字符:
String wordOne = "abc";
似乎在
“
和a
之间有一个隐藏的(不可打印的)字符。我尝试将您的示例粘贴到Eclipse中,它告诉我:
某些字符无法使用“Cp1252”字符编码进行映射
并指向字符串中的第一个字符:
String wordOne = "abc";
似乎在
“
和a
之间有一个隐藏(不可打印)字符,字符类型16对应于Unicode(U+202B)。这是一个无法打印的字符;您可以打印它的十六进制值以进行确认。字符类型16对应于Unicode(U+202B)。这是一个无法打印的字符;您可以打印它的十六进制值以确认。您的字符串包含一个无法看到的字符(在“a”之前)。Unicode集合中有几十个字符没有有意义的视觉表示——这可能是其中之一
“16”是字符类型,例如:
组合空格标点、连接符标点、控件、货币符号、破折号标点、十进制数字标点、封闭标点、尾端标点、末引号标点、格式、首引号标点、字母号、行分隔符、小写字母、数学符号、修饰字母、修饰字母、非空格标点、其他字母、其他数字、,其他标点符号、其他符号、段落分隔符、专用、空格分隔符、起始标点符号、替代项、标题字母、未指定、大写字母
所有这些都在
字符
类中定义。我不能告诉你它是哪一个,因为这在理论上依赖于实现;您应该对照这些值进行检查。或者,更好的方法是,使用Character.getName
查找该字符的可读描述。您的字符串包含一个难以看到的字符(在“a”之前)。Unicode集合中有几十个字符没有有意义的视觉表示——这可能是其中之一
“16”是字符类型,例如:
组合空格标点、连接符标点、控件、货币符号、破折号标点、十进制数字标点、封闭标点、尾端标点、末引号标点、格式、首引号标点、字母号、行分隔符、小写字母、数学符号、修饰字母、修饰字母、非空格标点、其他字母、其他数字、,其他标点符号、其他符号、段落分隔符、专用、空格分隔符、起始标点符号、替代项、标题字母、未指定、大写字母
所有这些都在
字符
类中定义。我不能告诉你它是哪一个,因为这在理论上依赖于实现;您应该对照这些值进行检查。或者,更好的方法是使用Character.getName
来查找人类可读的字符描述。啊,您的(几乎)完全正确。结果是《202d》。但是,这就把事情弄清楚了。谢谢,非常感谢。@1205526-啊,没错Character.getType()
实际上返回的是常规类别,而不是BiDi字符类型。(我讨厌这个方法的名字。)在这种情况下,一般类别16是,它包含了很多字符,包括U+202D(和U+202B)。啊,你的(几乎)完全正确。结果是《202d》。但是,这就把事情弄清楚了。谢谢,非常感谢。@1205526-啊,没错Character.getType()
实际上返回的是常规类别,而不是BiDi字符类型。(我讨厌这个方法名。)在本例中,通用类别16是,它包含很多字符,包括U+202D(和U+202B)。