String public int length public boolean equalsString s
String类常用方法 Øpublic int length() 获取字符串的长度 Øpublic boolean equals(String s) 比较当前字符串对象的实体是否与参数指定的字符串s的实体 相同 Øpublic boolean starts. With(String s) Øpublic boolean ends. With(String s) 判断当前字符串对象的前缀[后缀]是否是参数指定的字符串s Øpublic int compare. To(String s) 比较大小 [等 0;大 正;小 负] 2006. 02 By SCEMI XMJ
String s 1=new String(“中国. 四川”); String s 2=new String(“中国. 四川”); String s 3=new String(“中国”); String s 4=new String(“四川”); System. out. println(s 1. length()); //? System. out. println(s 3. length()); //? System. out. println(s 1==s 2); //? System. out. println(s 1. equals(s 2)); //? System. out. println(s 1=s 2); //? System. out. println(s 1==s 2); //? System. out. println(s 1. equals(s 2)); //? System. out. println(s 1. starts. With(s 3)); //? System. out. println(s 1. ends. With(s 4)); //? System. out. println(s 1. compare. To(s 2)); //? System. out. println(s 3. compare. To(s 4)); //? 5 2 false true 中国. 四川 true 0 -2222
String类常用方法 Øpublic int index. Of(String s[, int startpoint]) Øpublic int last. Index. Of(String s) 返回首次出现s的位置。[没有 -1]。 Øpublic char. At(int index) 返回第index个字符 Øpublic String substring(int startpoint[, int end]) 从start处截取到尾部[end处]所得到的字符串。 Øpublic String replace. All(String old , String new) 用参数new指定的字符串替换由old指定的所有字符串 Øpublic String trim() 去掉前后空格 2006. 02 By SCEMI XMJ
字符串与基本数据的相互转换 将基本类型数据转换为String型: String. value. Of(基本数据类型) 例:String. value. Of(0. 34) “ 0. 34” 将String型转换为基本类型数据:[03 -18提及] Integer. parse. Int(String s) …… Double. parse. Double(String s) 例:Double. parse. Double(“ 12. 34”) 12. 34 对象的字符串表示 对象名. to. String() Date date=new Date() date. to. String() 2006. 02 By SCEMI XMJ
String. Buffer类 创建String. Buffe对象 u. String. Buffer() u. String. Buffer(int size) u. String. Buffer(String s) length() 字符个数 capacity() 实际容量 String. Buffer sb=new String. Buffer() sb. length() //0 sb. capacity() //16 String. Buffer sb=new String. Buffer(32) sb. length() //0 sb. capacity() //32 String. Buffer sb=new String. Buffer(“SCEMI XMJ”) sb. length() //9 sb. capacity() //25=9+16 2006. 02 By SCEMI XMJ
String. Buffer类常用方法 Øappend [数据转化后]追加 Øvoid set. Char. At(int n, char ch) 用字符ch替换第n个字符 ØString. Buffer insert(int index, String str) 将字符串str插入从index起的当前字符串中 Øpublic String. Buffer reverse() 字符翻转 ØString. Buffer delete(int startpos, int endpos) 删除startpos到endpos-1子串 ØString. Buffer replace(int n , int m, String str) 用串str替换当前串从n到m-1的子串 2006. 02 By SCEMI XMJ
String. Buffer str=new String. Buffer(“ 123”) str. append(456); //对吗? 对 str=“ 123456” str=str+“ 789” //对吗? 错 str. append(“ 789”); //str? str=“ 12346789” System. out. println(str. set. Char. At(0, ‘a’)); //错 void str. set. Char. At(0, 'a'); //str? str=“a 2346789” System. out. println(str); //? ? str. insert(1, “***”); //str? str=“a***23456789” str. delete(1, 4); //str? str=“a 23456789” str. reverse(); //str? str=“ 98765432 a” str. replcae(0, 2, “###”); //对吗? 对 str=“###765432 a” str. replcae(5, 7, “##”); //str? str=“###76##32 a” str. length() //? 10 str. capacity() //? 19
Java Application命令行参数 public class Str. Array { public static void main(String[] args) { for(int i=0; i<args. length; i++) System. out. println(args[i]); } } javac Str. Array. java Str. Array 1 st 2 nd 3 rd 2006. 02 By SCEMI XMJ
if. . else… if(条件) 语句块 1 else 语句块 2 if(i%2==0) System. out. print(“偶数”); else System. out. print(“奇数”); 【示例】 简洁写法: 条件? 语句 1: 语句 2 2006. 02 By SCEMI XMJ
if…else if… if(条件 1) 语句块 1 else if(条件 2) 语句块 2 else if(条件 3) 语句块 3 else if(条件 4) 语句块 4 2006. 02 By SCEMI XMJ
循环结构 2006. 02 By SCEMI XMJ
Java语言的关键字 abstract do if boolean double implements private this break else import protected throw/throws byte extends instanceof public transient case false int return true catch final interface short try char finally long static void class float native strictfp volatile continue for new super while default goto * null switch package synchronized
- Slides: 51