Java基础之字符串操作(String)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22067469/article/details/82147216

将字节数组转成字符串

byte[] bytes = {97,98,99,100,101};
String s1 = new String(bytes);
System.out.println("s1: " + s1); //s1: abcde 

把字节数组的一部分转成字符串

byte[] bytes = {97,98,99,100,101};
String s2 = new String(bytes, 1, 3);
System.out.println("s2: " + s2);    //s2: bcd

把字符数组的一部份转成字符串

char[] chars = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String s3 = new String(chars, 2, 3);
System.out.println("s3: " + s3);    //s3: cde

判断定义为String类型的s1和s2是否相等

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);//比较的是内存地址
System.out.println(s1.equals(s2));//比较的是内容是否相等

String s1 = new String("abc");
String s2 = "abc";
//一个堆区地址,一个常量区地址,肯定不一样
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

//常量优化机制,编译的时候就已经是abc
/***
 * 最终.java编译成.class,再执行
 * .class
 * String s1 = "abc";
 * String s2 = "abc"
 */
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2);
System.out.println(s3.equals(s2));

判断字符串是否一样

String s1 = "abc";
String s2 = "ab" + "c";
System.out.println(s1.equals(s2));

判断字符串是否一样,忽略大小写

String s3 = "Abc";
System.out.println("s1与s3:" + s1.equals(s3));
System.out.println("s1与s3:" + s1.equalsIgnoreCase(s3));

判断字符串是否包含哪字符串

String s4 = "I Hate You!";
System.out.println(s4.contains("Love"));

判断字符串是否以什么开头

String s5 = "lucy";
System.out.println(s5.startsWith("A"));

判断字符串是否以什么结尾

String s6 = "lily.png";
System.out.println(s6.endsWith(".png"));

判断字符串是否为空字符串

String s7 = "java";
System.out.println(s7.isEmpty());

字符串比较一个小技巧

为了避免这个空指针异常,把字符串常量放在前面

if("admin".equals(username) && "admin".equals(password)){
    System.out.println("登录成功");
} else {
    System.out.println("登录失败");
}

获取字符串的长度

String s1 = "520java中";
System.out.println("s1的长度:" + s1.length() );

获取指定索引位置的字符

String s2 = "studio";
char c = s2.charAt(0);
System.out.println(c);

获取指定字符在此字符串第一次出现处的索引

String s3 = "eduationa";
System.out.println(s3.indexOf(97)); //'a'=97

返回指定字符串在此字符串中第一次出现处的索引

// 如果结果是-1,表示你指定的字符串没有出现过
String s4 = "WeLoveJava";
System.out.println(s4.indexOf("Love"));

返回指定字符在此字符串中指定位置后第一次出现处的索引

String s5 = "abcdefal";
System.out.println(s5.indexOf('a', 2));

返回指定字符串在此字符串中指定位置后第一次出现处的索引

String s6 = "I Love Java,I Love Gosling";
System.out.println(s6.indexOf("Love", 3));

最后出现的位置

String s7 = "ababaccad";
System.out.println(s7.lastIndexOf('a'));

从指定位置开始截取字符串,默认到未尾

String s8 = "Gosling,How Are You!";
String tmp = s8.substring(8);
System.out.println(tmp);

从指定位置开始到指定位置结束截取字符图

System.out.println(s8.substring(2, 7));

字符串的遍历

String s = "Gosling";
int len = s.length();//字符串的长度
for(int i = 0; i < s.length(); i++){
    System.out.println(s.charAt(i));
}

统计不同类型字符个数

String password = "abZZcNBiA21&*%sd^5";
// 大写,小写,数字,特殊字符
// 1.声明变量
int big = 0; //大写
int small = 0; // 小写
int num = 0; //数字
int other = 0; //特殊字符
// 2.遍历字符串的字符
for (int i = 0; i < password.length(); i++) {
    // 取字符
    char ch = password.charAt(i);
    // System.out.println(ch);
    // 判断字符是否为大写
    if (ch >= 'A' && ch <= 'Z') {
        big ++;
    } else if (ch >= 'a' && ch <= 'z') {// 判断字符是否为小写
        small ++;
    } else if (ch >= '0' && ch <= '9') {// 判断字符是否为数字
        num ++;
    } else {// 特殊字符
        other ++;
    }
}
// 输出
System.out.println("大写字母出现次数:" + big);
System.out.println("小写字母出现次数:" + small);
System.out.println("数字出现次数:" + num);
System.out.println("特殊字符出现次数:" + other);

把字符串转换为字节数组

String s1 = "520java";
byte[] bytes = s1.getBytes();
for(int i = 0; i < bytes.length;i++){
    System.out.println(bytes[i]);
}

把字符串转换为字符数组

String s2 = "java";
char[] chs = s2.toCharArray();
for(int i = 0; i < chs.length;i++){
    System.out.println(chs[i]);
}

把字符数组转成字符串

char[] chs2 = {'L','O','V','E'};
String s3 = String.valueOf(chs2);
System.out.println("s3=" + s3);

把int类型的数据转成字符串

// 注意:String类的valueOf方法可以把任意类型的数据转成字符串
int a = 97;
String s4 = String.valueOf(a);
System.out.println(s4);

把字符串转成小写

String s5 = "HELLO,HOW ARE YOU!";
s5 = s5.toLowerCase();
System.out.println("s5:" + s5);

把字符串转成大写

String s6 = "hi,麻辣鸡斯";
s6 = s6.toUpperCase();
System.out.println("s6:" + s6);

把字符串拼接,且只能拼接字符串。+方式拼接字符串时,可以是任意类型

String s7 = "When";
String s8 = " you go home?";
String s9 = s7.concat(s8);
//String s9 = s7 + s8 + 98;
System.out.println("s9:" + s9);

把一个字符串的首字母转成大写,其余为小写

String s = "hELLO, How Are You!";
s = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s);

把数组转成字符串

把数组中的数据按照指定的格式拼接成字符串
eg:int[] arr = {1,2,3} 转成 "[1,2,3]"
int[] arr = {1,2,3,4,5,6};
// 声明一个字符串
String str = "[";
for (int i = 0; i < arr.length; i++) {
    if (i == arr.length - 1) {//遍历到最后一次
        str += arr[i] + "]";
    } else {
        str += arr[i] + ",";
    }
}
System.out.println(str);

替换字符

String s1 = "hello";
s1 = s1.replace('l', 'y');
System.out.println("s1:" + s1);

替换字符串

//CharSequence,接口,字符系列
//String是CharSequence的实现类
String s2 = "I Love You!";
s2 = s2.replace("Love", "Hate");
System.out.println("s2:" + s2);

去除前后空格

String s3 = " Java Gosling ";
System.out.println("s3:" + s3);
s3 = s3.trim();
System.out.println("s3:" + s3);

比较字符串的大小

String s4 = "abc";
String s5 = "abc";
int result = s4.compareTo(s5);
System.out.println("s4 pk s5 = " + result);

String s6 = "AbB";
String s7 = "aBc";
System.out.println("s6 pk s7 = " + s6.compareToIgnoreCase(s7));

统计字符串出现的次数

String str = "nbacbakyriecbanbakyrie";
String name = "kyrie";
int count = 0;
while (true) {
    System.out.println(str);
    int index = str.indexOf(name);

    if (index == -1) {// 没有找到,退出循环
        break;
    }
    // 找到,截取字符串,在+1
    str = str.substring(index + name.length());
    count ++;
 }
 System.out.println(name + "出现在次数: " + count);// kyrie出现在次数: 2

面试题:数组有没有length方法,String中有没有length属性

/***
 * 数组是没有length方法,只有length属性
 * String是有length方法,而不是属性
 */
int[] arr = {1,2,3,4,5};
System.out.println(arr.length);

面试题:String这个类能不能被继承,或者说有没有子类?

/***
 * 答:String这个类是被final修饰的,所以不能被继承,也就是没有子类
 * 1.String 类代表字符串 。
 * 2.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 
 * 3.字符串是常量;它们的值在创建之后不能更改。
 */
String s1 = "abc";//字符串对象
//等效于
char data[] = {'a','b','c'};
String s2 = new String(data);
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
s1 = "bcd";

面试题:==号与equals方法的区别

/*
1.==是一个比较运算符号,即可以比较基本数据类型,也可以比较引用数据类据类型,基本数据类型比较的是值,引用数据类型比较的是地址值
2.equals方法是一个方法,只能比较引用数据类型,所有对象都会继承object类中的方法,
如果没有重写Object类中的equals方法,equals方法和==号比较引用数据类型无区别,
重写后的equals方法比较的是对象中的属性
*/

int a = 10;
int b = 10;
System.out.println("a和b是否相等:" + (a == b));

Student stu1 = new Student("小红", "中国");
Student stu2 = new Student("lily", "美国");
Student stu3 = stu1;
System.out.println("stu1 stu2 :" + stu1.equals(stu2));//默认比较是地址
System.out.println("stu1 stu2 :" + (stu1 == stu2));//这种写法也是比较地址
System.out.println("stu1 stu3 :" + (stu1 == stu3));

猜你喜欢

转载自blog.csdn.net/qq_22067469/article/details/82147216
今日推荐