2012-02-12 4 views
0

누구든지 여기에서 무슨 일이 벌어 질 수 있을지 생각하고 있습니까?String.charAt (0)가 아무 것도 출력하지 않고 문자 유형이 "16"이 될 수있는 원인은 무엇입니까?

첫 번째 블록은 내가 일반적으로 볼 것으로 예상되는 것을 보여줍니다. '문제'문자열이 색인 된 똑같은 것으로 대체되었지만 이전에는 결코 실행되지 않았던 문자열의 첫 번째 문자가 색인 '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"; // 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 
*/ 

제 2 블록이 주석 '새'문자열 및 'wordOne'의 첫 번째 문자가 아무것도 없다. null 문자 또는 개행 문자가 아닙니다. 그 변수를 사용하여 'theDoc'에서 쉼표를 찾았습니다 ...하지만 실행했을 때 '0'인덱스는 아무것도 유지하지 않았고 인덱스 1에는 쉼표가있었습니다. 문자열을 복사하여 붙여 넣으면 문제가 남아 있습니다. 그러나 주석을 달아서 삭제하면 문제를 해결할 수 있습니다. 이 같은 문제를 야기 자바에서 쉼표 나 기호에 대해 뭔가가

/* 
    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 
*/ 

있습니까 :

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)); 
} 

어느 출력을 제공? 나는 문자 배열을 사용하여 모든 것을 다시 빌드하기 위해 작업 공간을 청소 해 보았습니다. 그리고 아무것도 변경되지 않았습니다 ... 어떤 그램이 ", and"와 같은 경우에는 문장 내에서 'ngrams'색인을 찾는 데 큰 문제가 있습니다. 어느 날 밤 어젯밤에 일하고 있었고 갑자기 일이 시작되지 않았습니다. 나는 꽤 혼란 스럽다.

아이디어가 있으십니까?

감사합니다,

앤드류

답변

1

문자 유형 16은 유니 코드 DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING (U + 202B)에 해당합니다. 인쇄 할 수없는 문자입니다. 확인을 위해 16 진수 값을 출력 할 수 있습니다.

+0

아, 너의 (거의) 정확하게. 그것은 '202d'로 나왔다. 그러나 이것은 일을 깨끗이합니다. 고맙습니다, 많이 감사드립니다. – andrewm921

+0

@ user1205526 - 아 맞습니다. 'Character.getType()'은 실제로 BiDi 문자 유형이 아닌 일반 카테고리를 반환합니다. (메서드 이름이 싫다.)이 경우 일반 범주 16은 [FORMAT] (http://docs.oracle.com/javase/6/docs/api/java/lang/Character.html#FORMAT)이며 U + 202D (및 U + 202B)를 포함하여 꽤 많은 문자가 포함됩니다. –

2

나는 이클립스에 귀하의 예제를 붙여 시도하고이 나에게 말했다 :

일부 문자가 "Cp1252를"문자 인코딩을 사용하여 매핑 할 수 없습니다. "a 사이에 숨겨진 (비 인쇄) 문자가 나타납니다

String wordOne = "abc"; 

:

가와 문자열의 첫 번째 문자로 저를 지적했다.

0

문자열에 'a'앞에 표시하는 데 문제가있는 문자가 포함되어 있습니다. 유니 코드 세트에는 의미있는 시각적 표현이없는 수십 개의 문자가 있습니다. 아마도이 문자 중 하나 일 것입니다.

'16'은, 예를 들어, 상기 캐릭터 타입 :

COMBINING_SPACING_MARK, CONNECTOR_PUNCTUATION, CONTROL, CURRENCY_SYMBOL, DASH_PUNCTUATION, DECIMAL_DIGIT_NUMBER, ENCLOSING_MARK, END_PUNCTUATION, FINAL_QUOTE_PUNCTUATION, FORMAT, INITIAL_QUOTE_PUNCTUATION, LETTER_NUMBER, LINE_SEPARATOR, LOWERCASE_LETTER, MATH_SYMBOL, MODIFIER_LETTER, MODIFIER_SYMBOL, NON_SPACING_MARK, OTHER_LETTER, OTHER_NUMBER, OTHER_PUNCTUATION, OTHER_SYMBOL, PARAGRAPH_SEPARATOR, PRIVATE_USE, SPACE_SEPARATOR, START_PUNCTUATION 대리, TITLECASE_LETTER, 할당 해제 UPPERCASE_LETTER

이들은 모두 Character 클래스로 정의됩니다. 구현에 의존하는 이론이기 때문에 어떤 것인지 말할 수는 없습니다. 그 값들에 대해 점검해야합니다. 또는 더 나은 방법은 사람이 읽을 수있는 문자 설명을 찾기 위해 Character.getName을 사용하는 것입니다.