java string.ToCharArray()的使用方法
str.ToCharArray()这样一句,一时没明白是什么意思,所以就找了一下API,在API上的解释是“将此实例中的字符复制到 Unicode 字符数组。”,其实这是说的Unicode 字符就是用char定义的字符,其实不多说,举个例子,你就知道什么意思了。
例:
<blockquote>
public class Program
{
public static void main(String[] args)
{
String str = "This is a String.";
// Convert the above string to a char array.
char[] arr = str.toCharArray();
// Display the contents of the char array.
System.out.println(arr);
}
}
/*
Output:
This is a String.
再看
<blockquote>string chartest = "abcdefghijk";char[] chs = chartest.ToCharArray();//注:这里一定要用char来定义。
for (int i = 0; i < chs.Length; i )
{ Response.Write(chs[i] "");
}
</blockquote>
输出: a b c d e f g h i j k 看了这个例子你就知道了吧。
实例
/**
* @(#) ToCharArrayString.java
* ToCharArrayString class demonstrates the working of toCharArray() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/
<blockquote>public class ToCharArrayString {
public static void main(String args[]) {
//method converts complete String value to char array type value
String str = " einstein relativity concept is still a concept of great discussion";
char heram[] = str.toCharArray();
// complete String str value is been converted in to char array data by
// the method
System.out.print("Converted value from String to char array is: ");
System.out.println(heram);
}
} </blockquote>
Output of the program:-
Converted value from String to char array is: einstein relativity concept is still a concept of great discussion
您可能感兴趣的文章:
java string.ToCharArray()的使用方法
Java学习路径(二)书籍篇
php和java哪个好?
学习J2SE过程中的30个基本概念
Java的网络功能与编程 一
php可以和java一起开发吗
Java开发环境的过去、现在和将来
java String字符串解析实例整理
创建 Java .exe 文件
Java入门笔记1_HelloWorld