• 动手动脑之字符串


    1.动手动脑之String.equals()方法

    String.equals()的判等条件:用来检测两个String类型的对象是否相等,不能简单用“==”来判断两个字符串相等。

    public class fangfa {
        public static void main(String[] args)
        {
            String s1=new String("Hello");
            String s2=new String("Hello");
            System.out.println(s1==s2);
            System.out.println(s1.equals(s2));
            String s3="Hello";
            String s4="Hello";
            System.out.println(s3==s4);
            System.out.println(s3.equals(s4));
        }
    }

    例如此段代码,第一个输出的为false,其余都为true。因为String1和String2为两个对象,所以输出false。“==”指的是两个对象的引用相同,而“equals()”指的是两个对象的值相等。

    2.整理String类方法

    (1)Length():可以求出一个字符串的长度

             例:public int length()//求字符串长度

             String s=”asdfgh”;

             System.out.println(s.length());

    (2)char():获取制定位置的字符

             例:public charAt(int index)//index 是字符下标,返回字符串中指定位置的字符

            String s=”Hello”;

            System.out.println(s.charAt(3));

    (3)getChars():将字符从此字符串复制到目标字符数组

             例:public int getChars()//将字符从此字符串复制到目标字符数组

            String str = "abcdefghikl";

            Char[] ch = new char[8];

            str.getChars(2,5,ch,0);

    (4)replace():用于在字符串中用一些字符替换另一些字符

             例:public int replace()//替换字符串

            String s=”\”;

            System.out.println(s.replace(“\”,”///”));

    (5)toUpperCase():把字符串转化为大写

             例:public String toUpperCase()//将字符串全部转换成大写

             System.out.println(new String(“hello”).toUpperCase());

    (6)toLowerCase():把字符串转化成小写

             例:public String toLowerCase()//将字符串全部转换成小写

             System.out.println(new String(“HELLO”).toLowerCase());

    (7)trim():去掉头尾指针

             例:public String trim()

             String x=”ax  c”;

             System.out.println(x.trim());//是去两边空格的方法

    (8)toCharArray():将一个字符串内容转换为字符数组

             例:String x=new String(“abcd”);// 将字符串对象中的字符转换为一个字符数组

               char myChar[]=x.toCharArray();

              System.out.println(“myChar[1]”+myChar[1])

  • 相关阅读:
    tp5 select回显
    toFixed
    用js来实现银行家算法
    js 日期证有效性验的通用方法
    js获取或判断任意数据类类型的通用方法(getDataType)和将NodeList转为数组(NodeListToArray)
    js实现jquery函数animate动画效果
    js原生实现 无缝滚动图片
    scrollTop实现图像循环滚动(实例1)
    commonCookie.js
    delphi XE3解析JSON数据
  • 原文地址:https://www.cnblogs.com/lijing925/p/7736519.html
Copyright © 2020-2023  润新知