code

문자를 대문자로 변환

codestyles 2020. 10. 28. 08:07
반응형

문자를 대문자로 변환


String lower = Name.toLowerCase();
int a = Name.indexOf(" ",0);
String first = lower.substring(0, a);
String last = lower.substring(a+1);
char f = first.charAt(0);
char l = last.charAt(0);
System.out.println(l);

F 및 L 변수를 대문자로 변환하려면 어떻게해야합니까?


Character#toUpperCase()이것을 위해 사용할 수 있습니다 .

char fUpper = Character.toUpperCase(f);
char lUpper = Character.toUpperCase(l);

그러나 세계가 16 비트 char범위에 들어갈 수있는 것보다 더 많은 문자를 알고 있기 때문에 몇 가지 제한이 있습니다 . javadoc 의 다음 발췌 부분도 참조하십시오 .

참고 :이 방법은 보충 문자를 처리 할 수 ​​없습니다 . 보조 문자를 포함하여 모든 유니 코드 문자를 지원하려면 toUpperCase(int)메서드를 사용하십시오 .


기존 유틸리티를 사용하는 대신 부울 연산을 사용하여 아래 변환을 시도 할 수 있습니다.

대문자로 :

 char upperChar = 'l' & 0x5f

소문자로 :

   char lowerChar = 'L' ^ 0x20

작동 원리 :

2 진, 16 진 및 10 진수 테이블 :

------------------------------------------
| Binary   |   Hexadecimal     | Decimal |
-----------------------------------------
| 1011111  |    0x5f           |  95     |
------------------------------------------
| 100000   |    0x20           |  32     |
------------------------------------------

small lto Lconversion 의 예를 살펴 보겠습니다 .

바이너리 AND 연산 : (l & 0x5f)

l문자는 ASCII 108이고 01101100이진 표현입니다.

   1101100
&  1011111
-----------
   1001100 = 76 in decimal which is **ASCII** code of L

유사하게 Lto l변환 :

바이너리 XOR 연산 : (L ^ 0x20)

   1001100
^  0100000
-----------
   1101100 = 108 in decimal which is **ASCII** code of l

java.lang.Character클래스를 살펴보면 문자를 변환하거나 테스트하는 데 유용한 많은 메서드를 제공합니다.


f = Character.toUpperCase(f);
l = Character.toUpperCase(l);

System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());

프로젝트에 apache commons lang jar를 포함하는 경우 가장 쉬운 해결책은 다음과 같습니다.

WordUtils.capitalize(Name)

당신을 위해 모든 더러운 일을 처리합니다. 여기 에서 javadoc을 참조 하십시오.

또는 나머지 문자도 소문자로 지정하는 capitalizeFully (String) 메서드도 있습니다.


문자가 소문자임을 알고 있으므로 해당 ASCII 값을 빼서 대문자로 만들 수 있습니다.

char a = 'a';
a -= 32;
System.out.println("a is " + a); //a is A

다음은 참조를 위한 ASCII 테이블 입니다.


You can apply the .toUpperCase() directly on String variables or as an attribute to text fields. Ex: -

String str;
TextView txt;

str.toUpperCase();// will change it to all upper case OR
txt.append(str.toUpperCase());
txt.setText(str.toUpperCase());

I think you are trying to capitalize first and last character of each word in a sentence with space as delimiter.

Can be done through StringBuffer:

public static String toFirstLastCharUpperAll(String string){
    StringBuffer sb=new StringBuffer(string);
        for(int i=0;i<sb.length();i++)
            if(i==0 || sb.charAt(i-1)==' ' //for first character of string/each word
                || i==sb.length()-1 || sb.charAt(i+1)==' ') //for last character of string/each word
                sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
     return sb.toString();
}

The easiest solution for your case - change the first line, let it do just the opposite thing:

String lower = Name.toUpperCase ();

Of course, it's worth to change its name too.

참고URL : https://stackoverflow.com/questions/3696441/converting-a-char-to-uppercase

반응형