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


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


Character 类的 toString() 方法返回表示给定 Character 值的 String 对象。获得的结果通常是长度为 1 的字符串,其组件是表示 Character 对象的原始 char 值。

用法

public String toString()

返回值

toString() 方法返回给定字符的字符串表示。

例子1

public class JavaCharacterToStringExample1 {
  public static void main(String[] args) {
      // Create two Character objects c1 and c2.
      Character c1, c2;
      // Assign the values to c1 and c2.
      c1 = new Character('r');
      c2 = new Character('9');
      // Create two String objects s1 and s2.
      String s1, s2;
      // Assign the String values of c1 and c2 to s1 and s2 respectively.
      s1 = c1.toString();
      s2 = c2.toString();
      String str1 = "The string value of " + c1 + " is:" + s1;
      String str2 = "The string value of " + c2 + " is:" + s2;
      // Print the values of str1 and str2.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The string value of r is:r
The string value of 9 is:9

例子2

public class JavaCharacterToStringExample2 {
  public static void main(String[] args) {
      // Create two Character objects c1 and c2.
      Character c1, c2;
      // Assign the values to c1 and c2.
      c1 = new Character('A');
      c2 = new Character('7');
      // Create two String objects s1 and s2.
      String s1, s2;
      // Assign the String values of c1 and c2 to s1 and s2 respectively.
      s1 = c1.toString();
      s2 = c2.toString();
     // Print the values of str1 and str2.
      System.out.println("The string value of " + c1 + " is:" + s1);
      System.out.println("The string value of " + c2 + " is:" + s2);
     
   }
}

输出:

The string value of A is:A
The string value of 7 is:7

Java 字符 toString(char c) 方法

Character 类的 toString(char c) 方法返回表示给定 Character 值的 String 对象。获得的结果通常是长度为 1 的字符串,其组件是表示 Character 对象的原始 char 值。

用法

public static String toString(char c)

参数

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

返回值

toString(char c) 方法返回给定字符的字符串表示形式。

例子1

public class JavaCharacterToStringExample_1 { 
   public static void main(String[] args) {
    // Create two char primitives ch1 and ch2.
      char ch1, ch2;
    // Assign the values to ch1 and ch2.
      ch1 = 'V';
      ch2 = 118;
    // Create two String objects s1 and s2.
      String s1, s2;
    // Assign the String values of ch1 and ch2 to s1 and s2.
      s1 = Character.toString(ch1);
      s2 = Character.toString(ch2);
      String str1 = "The string value of " + ch1 + " is given as:" + s1;
      String str2 = "The string value of " + ch2 + " is given as:" + s2;
    // Print the values.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The string value of V is given as:V
The string value of v is given as:v

例子2

public class JavaCharacterToStringExample_2 {
   public static void main(String[] args) {
    // Create two char primitives ch1 and ch2.
      char ch1, ch2;
    // Assign the values to ch1 and ch2.
      ch1 = 'f';
      ch2 = 'e';
    // Create two String objects s1 and s2.
      String s1, s2;
    // Assign the String values of ch1 and ch2 to s1 and s2.
      s1 = Character.toString(ch1);
      s2 = Character.toString(ch2);
      String str1 = "The string value of " + ch1 + " is given as:" + s1;
      String str2 = "The string value of " + ch2 + " is given as:" + s2;
    // Print the values.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The string value of f is given as:f
The string value of e is given as:e




相关用法


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