最近面试了两道题目,这两道题目有点像,放在一起。
第一道题目以为结果会输出”beeB”,结果还是我太菜了,输出结果还是BEA。一句话,两个str的内存地址不一样
public void main(){
String str="BEA";
this.modify(str);
System.out.println(str);//BEA
}
public void modify(String str) {
str.replace('A', 'E');
str.toLowerCase();
str+='B';
}
public static void main(String[] args) {
Demo3 demo3 = new Demo3();
demo3.main();
}
public class StringTest06 {
String str = new String("good");
char[ ] ch = { 'a' , 'b' , 'c' };
public static void main(String args[]){
StringTest06 ex = new StringTest06();
ex.change(ex.str,ex.ch);
System.out.print(ex.str + " and ");//goodandgbc
System.out.print(ex.ch);
}
public void change(String str,char ch[ ]){
str = "test ok";
ch[0] = 'g';
}
}
String
public class StringTest01 {
public static void main(String[] args) {
//这两行代码表示底层创建了3个字符串对象,都在字符串常量池中
String s1="abcdef";
String s2="abcdef"+"xy";
System.out.println(s1==s2);//false
//这是使用new方式创建的字符串对象,这个代码中的"xy"是从哪里来的
//凡是双引号括起来的都在字符串常量池中有一份
//new对象的时候一定在堆内存中开辟空间
String s3 = new String("xy");
//i变量中保存的是100这个值
int i=100;
//s变量中保存的是字符串对象的内存地址
//s引用中保存的不是"abc",是0x1111
//而0x1111是"abc"字符串对象在字符串常量池当中的内存地址
String s="abc";
}
}
String s1 = new String("hello");
String s2 = new String("hello");
String常用的构造方法
public class StringTest04 {
public static void main(String[] args) {
//创建字符串对象最常用的一种方式
String s1="abcde";
//s1保存的是内存地址
//toString重写了
System.out.println(s1);
byte[]bytes={97,98,99};//abc
String s2 = new String(bytes);
System.out.println(s2.toString());//abc
System.out.println(s2);//abc
//String(字节数组,起始数组下标,数组长度)
//将byte数组一部分转换成字符串
String s3 = new String(bytes,1,2);
System.out.println(s3);//bc
//将char数组全部转换成字符串
char[] chars ={'我','爱','你','中','国'};
//将char数组转化成字符串
String s4 = new String(chars);
System.out.println(s4);//我爱你
//将char数组一部分转化成字符串
String s5 = new String(chars,1,2);
System.out.println(s5);//爱你
System.out.println(new String("hello"));//hello
}
}
String类中常用的方法
//String类中常用的方法
public class StringTest05 {
public static void main(String[] args) {
//char.charAt(int index)
char c = "中国人".charAt(1);
System.out.println(c);//国
//判断前面的字符串是否包含后面的字符串contains()
System.out.println("hello.java".contains("hello"));//true
//判断是否以某个字符串结尾
System.out.println("hello.java".endsWith(".java"));//true
System.out.println("java.text".endsWith(".text"));//true
System.out.println("hello.text".endsWith("xt"));
System.out.println("====================================");
// //判断是否以某个字符串开始
System.out.println("hello.java".startsWith("hello"));//true
System.out.println("hello.java".startsWith("el"));//false
//判断相等
System.out.println("ABC".equals("abc"));//false
//忽略大小写equalsIgnoreCase()
System.out.println("ABC".equalsIgnoreCase("abc"));//true
//将字符串对象转换成字节数组getBytes()
byte[] bytes = "abcde".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
//判断某个字符串在当前字符串中第一次出现的索引indexOf()
System.out.println("abcdefgjavaabcder".indexOf("java"));//7
//判断某个字符串在当前字符串中最后一次出现的索引lastIndexOf()
System.out.println("ahsdhshdjasdasjavadajsdasjdhjavadjo".lastIndexOf("java"));//28
//判断某个字符串是否为空字符串,isEmpty(),底层调用的是length方法
String s="";
System.out.println(s.isEmpty());//true
//int length
//判断数组长度是length属性,判断字符串长度是length()方法
System.out.println("abc".length());
//replace替换
String newString="http://www.baidu".replace("http","https");
System.out.println(newString);//https://www.baidu
//拆分
String[] split = "1998-9-29".split("-");//以-分隔符进行拆分
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
//截取字符串,参数是起始下标
System.out.println("sdwuhduwhduw".substring(5));//duwhduw
//截取字符串[begin,end)
System.out.println("abcdefghijk".substring(5,8));//fgh
//字符串转化成新的字符数组
char[] chars = "我是中国人".toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
//toLowerCase()转换成小写
System.out.println("ABCDEFG".toLowerCase());//abcdefg
System.out.println("abcdefr".toUpperCase());//ABCDEFR
//去除前后空白
System.out.println(" hello world ".trim());//hello world
}
}



