1 字符串处理
取串。 如helloworld 截取world
String str="HelloWorld";
//取world 5<=n<9 前闭后开
str=str.substring(5,10);
System.out.println(str);
}
测试字符串是否相等. equals
object equals 等价于== :比较内存地址
String equals :比较内容
package day11;
public class TestString2 {
public static void main(String[] args) {
String s1="hello";//声明一个String类型的变量
String s2="hello";//声明另一个内容相同的String类型的变量
String s3="hello"+"world";
//比较的是内容
System.out.println(s1.equals(s2));
//比较的是内存地址
System.out.println(s1==s2);
//比较的是内容
System.out.println(s1.equals(s3));
//比较的是内存地址
System.out.println(s1==s3);
}
}
1.寄存器
2. 栈
3.堆 new
4.静态存储器
5. 常量存储器 final static String 常量池
API http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
package day11;
public class TestString3 {
public static void main(String[] args) {
//维护在常量池里面
String a="hello";
String b="hello";
//new出来的所有对象都是堆内存
//只要是new出来的,都是新对象
String c =new String("hello");
String d =new String("hello");
System.out.println(a==b);
System.out.println(a==c);
System.out.println(c==d);
}
}
字符串编辑
字符串的内容不会变,改变的是引用
public class TestString4 {
public static void main(String[] args) {
String a="hello";
a=a+"\t world";
System.out.println(a);
}
}
2 字符串其他常用操作
利用 API进行学习
String a="hello";
a=a+"\t world";
System.out.println(a);
int count=a.indexOf("o");
System.out.println(count);