标签:blog http os 使用 ar for strong sp 数据
String是类,不是基本数据类型。
1. String s=“hello”;
2. String s=new String(“hello”);
3. char chars[]={‘x‘,‘y‘,‘z‘};
String s1=new String(chars);
String s2=new String(chars,0,2);
==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象
equals():检查组成字符串内容的字符是否完全一致
String s = new String("你好,");
String name = new String("张三!");
String sentence = s.concat(name);
System.out.println(sentence);
用于存储和操作由多个字符组成的可变数据,通常用来动态的构造字符数据。
StringBuffer st=new StringBuffer(); // 创建StringBuffer 空对象
StringBuffer st=new StringBuffer(10); //创建具有指定初始长度的对象
StringBuffer st=new StringBuffer(“aaa”); //创建一个变量储存字符串aaa
St.toString(); //转化为String类型
ST.append(“***”) ; 追加字符串
StringBuffer 可变字符串 线程安全,多线程
StringBuilder 速度最快,单线程
public class String01 {
public static void main(String[] args) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder s = new StringBuilder();
int len = 4;
for(int i=0;i<=len;i++){
Random r = new Random();
int p = r.nextInt(str.length());
s.append(str.charAt(p));
}
System.out.println(s.toString());
}
}
public class String02 {
public static void main(String[] args) {
String fn = "aaa-ffff.jpg";
String ext = fn.substring(fn.lastIndexOf(‘.‘));
String newfile = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date())+ext;
String n2 = UUID.randomUUID().toString()+ext;
System.out.println(n2);
}
}
// UUID 随机生成数一组字符串且不同
//格式化时间SimpleDateFormat("yyyyMMddhhmmssS").format(new Date())
public class String03 {
public static void main(String[] args) {
String week = "日,一,二,三,四,五,六";
String[] wk = week.split(","); //根据给定正则表达式的匹配拆分此字符串。
//Date d = new Date();
//String n = "今日是:星期"+wk[d.getDay()];
Calendar c = Calendar.getInstance();
//System.out.println(c.get(Calendar.DAY_OF_WEEK));
String n = "今日是:星期"+wk[c.get(Calendar.DAY_OF_WEEK)-1];
System.out.println(n);
}
}
标签:blog http os 使用 ar for strong sp 数据
原文地址:http://www.cnblogs.com/Dreamlu/p/4025069.html