字符串初始化、查找字符+获取字符

复制代码
public class zifuchuan {

    public static void main(String[] args) {
        
        
        //字符串初始化.
        String a="字符串";//内存空间地址一样
        String d="字符串";
        String b=new String("字符串");//new,开辟新的内存空间
        String c=new String("字符串");
        char[]e=new char[]{'我','很','好'};
        String f=new String(e);
        System.out.println(f);
        
        
        
        //==运算,比较的是内存地址。
        System.out.println(a==b);
        System.out.println(a==c);
        System.out.println(a==d);
        System.out.println(b==c);
        
        
        //比较字符串是否相等。
        System.out.println(a.equals(b));
        System.out.println(b.equals(c));
        
        
        
        
        
        //字符串信息
        a.length();//带()的是方法,不带的是属性
        
        //indexOf(),只能从前往后找:
        
        //查找字符串位置,查找字符串中子字符串的位置,返回值是找到之后的索引值。
        System.out.println(a.indexOf("串"));//索引的位置。
        System.out.println(a.indexOf("符串"));//首字的索引值
        
        //查找不到时:
        System.out.println(a.indexOf("无"));//返回值是-1
        
        //有多个时,只找第一个所在的索引值:
        String g=new String("字符串字符串字符串");
        System.out.println(a.indexOf("串"));
        
        
        //lastIndexOf();从后边往前找
        
        System.out.println(g.lastIndexOf("符"));
        
        
        //获取字符.charAt(索引)
        char c1=g.charAt(2);
        System.out.println(c1);
        
复制代码

 

posted @   百事没事  阅读(239)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示