前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第十四届蓝桥杯集训——字符串函数(基础函数操作)

第十四届蓝桥杯集训——字符串函数(基础函数操作)

作者头像
红目香薰
发布2023-01-10 13:28:06
2660
发布2023-01-10 13:28:06
举报
文章被收录于专栏:CSDNToQQCodeCSDNToQQCode

第十四届蓝桥杯集训——字符串函数(基础函数操作)


目录

 第十四届蓝桥杯集训——字符串函数

String的由来

String 和 new String的区别

1、创建的字符串变量在内存中的区别。

2、特性不同:

3、性能效率不同:

4、是否相同判断

常用字符串列表

substring

replace

trim

toCharArray

toLowerCase与toUpperCase

indexOf

split


String的由来

通过以下编码可以看出,字符串实际就是字符数组。

代码语言:javascript
复制
char chars[]={'a','b','c'};
String s=new String(chars);
System.out.println(s);
int len=s.length();
System.out.println(len);

字符串转成byte数组

代码语言:javascript
复制
String s = "Hello world";
byte[] bytes = s.getBytes();
for (byte b : bytes) {
    System.out.print((char)b);
}

String 和 new String的区别

1、创建的字符串变量在内存中的区别。

两者看似都是创建了一个字符串对象,但在内存中确是各有各的想法。

String str1= “abc”; 在编译期,JVM会去常量池来查找是否存在“abc”,如果不存在,就在常量池中开辟一个空间来存储“abc”;如果存在,就不用新开辟空间。然后在栈内存中开辟一个名字为str1的空间,来存储“abc”在常量池中的地址值。

String str2 = new String("abc") ;在编译阶段JVM先去常量池中查找是否存在“abc”,如果过不存在,则在常量池中开辟一个空间存储“abc”。

在运行时期,通过String类的构造器在堆内存中new了一个空间,然后将String池中的“abc”复制一份存放到该堆空间中,在栈中开辟名字为str2的空间,存放堆中new出来的这个String对象的地址值。

也就是说,前者在初始化的时候可能创建了一个对象,也可能一个对象也没有创建;后者因为new关键字,至少在内存中创建了一个对象,也有可能是两个对象。

2、特性不同:

String类 是final修饰的,不可以被继承。

String类的底层是基于char数组的。

3、性能效率不同:

String类被设计成不可变(immutable)类,所以它的所有对象都是不可变对象。例如: String str = “hello"; str = str + "world“; 所以当上文str指向了一个String对象(内容为“hello”),然后对str进行“+”操作,str原来指向的对象并没有变,而是str又指向了另外一个对象(“hello world”),原来的对象还在内存中。 由此也可以看出,频繁的对String对象进行修改,会造成很大的内存开销。此时应该用StringBuffer或StringBuilder来代替String。

而new String()更加不适合,因为每一次创建对象都会调用构造器在堆中产生新的对象,性能低下且内存更加浪费。

4、是否相同判断

String s0="abc",s1="abc" ; if(s0==s1){} 结果为true 因为在java中字符串的值是不可改变的,相同的字符串在内存中只会存一份,所以a和b指向的是同一个对象。

String s2=new String("abc"); String s3=new String("abc"); if(s2==s3){} 结果为false,此时a和b指向不同的对象

String s2=new String("abc"); String s3=new String("abc"); if(a.equals(b)){} 结果为true

代码语言:javascript
复制
package com.item.action;

public class Demo {

	public static void main(String[] args) {
		String s0="abc";
		String s1="abc";
		String s2=new String("abc");
		String s3=new String("abc");
		System.out.println(s0==s1);
		System.out.println(s1==s2);
		System.out.println(s2==s3);
		System.out.println(s0.hashCode());
		System.out.println(s1.hashCode());
		System.out.println(s2.hashCode());
		System.out.println(s3.hashCode());
		System.out.println(s0.equals(s1));
		System.out.println(s1.equals(s2));
		System.out.println(s2.equals(s3));
	}

}

输出结果: 

常用字符串列表

length()//取得字符串的长度 substring()//字符串截取 concat() //连接两个字符串 replace()//替换 trim()//去掉起始和结尾的空格 valueOf()//转换为字符串 toLowerCase()//转换为小写 toUpperCase()//转换为大写 toCharArray()//转char数组 equals()//比较两个字符串区分大小写 equalsIgnoreCase()//比较两个字符串不区分大小写 indexOf()//查找字符或者子串第一次出现的地方 lastIndexOf()//查找字符或者子串是后一次出现的地方 split()//字符串分割

substring

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
        String str="I HAVE A DREAM!";
        String s = str.substring(2, 2+4);
        System.out.println(s);
    }
}

replace

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
        String str="I HAVE A DREAM!";
        String s = str.replace("DREAM", "GOOD IDEA");
        System.out.println(s);
    }
}

trim

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
        String str="\tI HAVE A DREAM!\t";    
        String s = str.trim();
        System.out.println(s);
    }
}

toCharArray

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
        String str = "89dsa dady8)ILuhd9usa)(*YGIUhdusa hoi";
        char[] array = str.toCharArray();
        int low = 0;
        int up = 0;
        int num = 0;
        int other = 0;
        for (char c : array) {
            if (c >= 'a' && c <= 'z') {
                low++;
            } else if (c >= 'A' && c <= 'Z') {
                up++;
            } else if (c >= '0' && c <= '9') {
                num++;
            } else {
                other++;
            }
        }
        System.out.println(str.length());
        System.out.println(low);
        System.out.println(up);
        System.out.println(num);
        System.out.println(other);
    }
}

toLowerCase与toUpperCase

代码语言:javascript
复制
public class Demo {
    public static void main(String[] args) {
        String str="I HAVE A DREAM!";
        String lowerCase = str.toLowerCase();
        System.out.println(lowerCase);
        String upperCase = str.toUpperCase();
        System.out.println(upperCase);
    }
}

indexOf

代码语言:javascript
复制
import java.util.UUID;
 
public class Demo {
    public static void main(String[] args) {
        String str = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = str.concat(".jpg");
        int indexOf = fileName.indexOf(".jpg");
        System.out.println(indexOf);
        System.out.println(fileName.substring(indexOf,fileName.length()));
    }
}

split

代码语言:javascript
复制
import java.util.UUID;
 
public class Demo {
    public static void main(String[] args) {
        String str = UUID.randomUUID().toString();
        System.out.println(str);
        String[] split = str.split("-",str.length());
        for (String string : split) {
            System.out.println(string);
        }
    }
}

后续会有对应的联系题目。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第十四届蓝桥杯集训——字符串函数(基础函数操作)
  • String的由来
  • String 和 new String的区别
    • 1、创建的字符串变量在内存中的区别。
      • 2、特性不同:
        • 3、性能效率不同:
          • 4、是否相同判断
          • 常用字符串列表
          • substring
          • replace
          • trim
          • toCharArray
          • toLowerCase与toUpperCase
          • indexOf
          • split
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档