发表于: 2017-02-06 23:52:12
2 2000
今天完成的事情:
1.创建字符串
1.char a[]={'a','b','c'}
String s = new String(a) 等价于 String s = new String("abc")
2.char a[]={'a','b','c','d','e'}
String s = new String(a,2,4) 等价于String s =new String("bcde") a,2,4表示从a数组第二号位置开始取4个字符组成字符串
2.字符串连接
public class date9{
public static void main(string args[]){
string s1 = new string("hello"); //声明字符串并赋值
string s2 = new string ("java");
string s3 = s1 +“” + s2; // 输出时,s1与s2之间带空格
System.out.println(s);
}
}
3.字符串与其他数据连接时
"hello java" +1+a 输出为hello java1a 字符串与任意数据相连时 组成新的字符串
1+a+"hello java" 输出为98hello java 从左到右读数,char a转换为int数据(97 +1)+hello java输出98hello java
4.字符位置检索
1.str.indexOf()
String str = "we are studengs";
int size=str.indexOf("t") 输出8(字符串下标为0到length()-1
5.获得指定位置的字符
String str = "we are studengs";
char mychar=str.charAt(3) 输出a
6.获取指定位置字符串
String str = "we are studengs";
string substr=str.substring(3,5) 输出are //去掉5则为are studengs
明天计划的事情:字符串替换,判断字符串的开始与结尾,判断字符串是否相等,字符串分割,
遇到的问题:无
收获:indexOf()从头查找,lastindiexOf()从尾查找,charAt()获得当前位置的字符,substring(,)截取位置到位置的字符串,
评论