java 字符串,字符数组,list间的转化,字符串拼接

1.关于java.lang.string.split
xxx.split()方法可以将一个字符串分割为子字符串,然后将结果作为字符串数组返回。

2.字符串转字符数组
String str =" aa.png,a2.png,a3.png";
String[] arrayStr =new String[]{};
arrayStr = str.split(",");

3.字符数组转list
List list = java.util.Arrays.asList(arrayStr);

4.list转化为字符数组
String[] storeStr = list.toArray(new String[list.size()]);

public class SplitTest {
public static void main(String args[]) {
String str = "a1.png,a2.png,a3.png";// 原始字符串
String[] arrayStr = new String[] {};// 字符数组
List<String> list = new ArrayList<String>();// list

arrayStr = str.split(",");// 字符串转字符数组
list = java.util.Arrays.asList(arrayStr);// 字符数组转list
String[] storeStr = list.toArray(new String[list.size()]);// list转成字符数组

/* 输出字符数组的值 */
for (String s : arrayStr) {
System.out.println(s);

}
System.out.println("-------------");
/* 输出list值 */
for (String s : list) {
System.out.println(s);

}
System.out.println("-------------");

for (String s : storeStr) {
System.out.println(s);

}
System.out.println("-------------");
}
}

5.Java 5种字符串拼接方式性能比较

  • 用+的方式:编译器将构造一个StringBuffer对象,并调用其append方法。效率最差
  • concat由于是内部机制实现,比+的方式好了不少
  • Join 和 StringBuffer,相差不大,Join方式要快些,可见这种JavaScript中快速拼接字符串的方式在Java中也非常适用。
  • StringBuilder 的速度最快,但其有线程安全的问题,而且只有JDK5支持
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: