zoukankan      html  css  js  c++  java
  • 字符串初始化、查找字符+获取字符

    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);
            
  • 相关阅读:
    Cat- Linux必学的60个命令
    Cmp- Linux必学的60个命令
    Diff- Linux必学的60个命令
    ls- Linux必学的60个命令
    mv- Linux必学的60个命令
    Find- Linux必学的60个命令
    libvirt
    PHP 设计模式 笔记与总结(2)开发 PSR-0 的基础框架
    Java实现 LeetCode 147 对链表进行插入排序
    Java实现 LeetCode 146 LRU缓存机制
  • 原文地址:https://www.cnblogs.com/1ming/p/5228056.html
Copyright © 2011-2022 走看看