当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character toLowerCase()用法及代码示例


Character 类的 toLowerCase(char ch) 方法使用 Unicode 数据文件提供的大小写映射信息将给定的字符参数转换为小写。应该注意的是 Character.isLowerCase(Character.toLowerCase(ch)) 对于某些字符(如符号或表意文字)可能并不总是返回 true。

实际上,可以使用 String.toLowerCase() 将字符映射为小写。与字符大小写映射相比,字符串大小写映射有很多好处。字符串大小写映射可用于执行 local-sensitive 映射、context-sensitive 映射,而不能使用字符大小写映射。

用法

public static char toLowerCase(char ch)

参数

ch: 这是需要测试的字符。

返回值

toLowerCase(char ch) 方法返回给定字符的小写(如果有)。否则,此方法返回字符本身。

例子1

public class JavaCharactertoLowerCaseExample1 {
    public static void main(String[] args) {
      // Create four char primitives.
      char ch1, ch2, ch3, ch4;
      // Assign the values to ch1 and c.
      ch1 = 'F';
      ch2 = 'n';
      // Assign the lowercase of ch1 and ch2 to ch and ch4 respectively.
      ch3 = Character.toLowerCase(ch1);
      ch4 = Character.toLowerCase(ch2);
      String str1 = "The lowercase for the first character '" + ch1 + "' is given as:" + ch3;
      String str2 = "The lowercase for the second character '" + ch2 + "' is given as:" + ch4;
      // Print the values for ch3 and ch4.
      System.out.println( str1 );
      System.out.println( str2 );
      }
}

输出:

The lowercase for the first character 'F' is given as:f
The lowercase for the second character 'n' is given as:n

例子2

import java.util.Scanner;
public class JavaCharactertoLowerCaseExample2 {
  public static void main(String[] args) {
    // Ask the user to enter the first input.
       System.out.print("Enter the first input:");
    // Use Scanner class to get the user.
       Scanner s = new Scanner(System.in);
       char[] value1 = s.nextLine().toCharArray();
    // Convert the given user input into lower case.
      for(char ch1:value1){
      char lower1 = Character.toLowerCase(ch1);
    // Print the result
       System.out.println("The character "+ ch1 +" is converted in the lowercase as:" +lower1); 
    // Ask the user to enter the second input.
       System.out.print("Enter the second input:");          
        char[] value2 = s.nextLine().toCharArray();
        for(char ch2:value2){
        char lower2 = Character.toLowerCase(ch2);
        System.out.println("The character "+ ch2 +" is converted in the lowercase as:" +lower2);}		
	}
    } 
}

输出:

Enter the first input:T
The character T is converted in the lowercase as:t
Enter the second input:i
The character i is converted in the lowercase as:i

Java 字符 toLowerCase()Method

Character 类的 toLowerCase(int codePoint) 方法使用由 Unicode 数据文件提供的大小写映射信息将给定的字符(Unicode 代码点)参数转换为小写。

应该注意的是 Character.isLowerCase(Character.toLowerCase(codePoint)) 对于某些字符(如符号或表意文字)可能并不总是返回 true。

用法

public static int toLowerCase(int codePoint)

参数

上述方法只需要一个参数:

a.)codePoint(Unicode code point),即需要测试的字符。

返回值

toLowerCase(int codePoint) 方法返回给定字符的小写(如果有)。否则,此方法返回字符本身。

例子1

public class JavaCharactertoLowerCaseExample_1 {
  public static void main(String[] args) {
    // initialize two codePoints.
       int codepoint1 = 87;
       int codepoint2 = 90;
    //  Convert the codepoints to char primitives.
        char ch1 = (char)codepoint1;
        char ch2 = (char)codepoint2;
    // Convert the code point to lower case respectively.
	char lower1 = Character.toLowerCase(ch1);
        char lower2 = Character.toLowerCase(ch2);
    // Print the result.
System.out.println("The character '" + ch1 + "' can be represented in lower case as " + lower1);
System.out.println("The character '" + ch2 + "' can be represented in lower case as " + lower2);
	}
}

输出:

The character 'W' can be represented in lower case as w
The character 'Z' can be represented in lower case as z

例子2

public class JavaCharactertoLowerCaseExample_2 {
  public static void main(String[] args) {
    // initialize two codePoints.
       int codepoint1 = 121;
       int codepoint2 = 95;
    //  Convert the codepoints to char primitives.
        char ch1 = (char)codepoint1;
        char ch2 = (char)codepoint2;
    // Convert the code point to lower case respectively.
        char lower1 = Character.toLowerCase(ch1);
        char lower2 = Character.toLowerCase(ch2);
    // Print the result.
    String str1 = "The character '" + ch1 + "' can be represented in lower case as " + lower1;
    String str2 = "The character '" + ch2 + "' can be represented in lower case as " + lower2;
    System.out.println(str1);
    System.out.println(str2);
	}
}

输出:

The character 'y' can be represented in lower case as y
The character '_' can be represented in lower case as _




相关用法


注:本文由纯净天空筛选整理自 Java Character toLowerCase() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。