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 toTitleCase()用法及代碼示例
- Java Character toUpperCase()用法及代碼示例
- Java Character toChars()用法及代碼示例
- Java Character toCodePoint()用法及代碼示例
- Java Character toLowerCase()用法及代碼示例
- Java Character isLetter()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isValidCodePoint()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- Java Character getType()用法及代碼示例
- Java Character getNumericValue()用法及代碼示例
- Java Character isSpace()用法及代碼示例
- Java Character isMirrored()用法及代碼示例
- Java Character isBmpCodePoint()用法及代碼示例
- Java Character isIdentifierIgnorable()用法及代碼示例
- Java Character isDigit()用法及代碼示例
- Java Character digit()用法及代碼示例
- Java Character compare()用法及代碼示例
- Java Character isLowerCase()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character toString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。