Java学习第四天 String类

/*
* 获取字符串长度 String.length()
* 字符串区分大小写比较 String.equals(String str) 返回true或false
* 字符串不区分大小写比较 String.equalsIgnoreCase(String str);返回true或false
* 返回指定索引处的字符 String.charAt(int index)
* 查找参数字符串在原字符串中首次出现的位置 String.indexOf(String str)找到返回所在位置的下标,未找到返回-1
* 字符串的拼接 String.concat(String str) 返回拼接后的新字符串对象
* 字符串在程序运行期间,其值不可改变.所以也叫字符串常量.
* 字符串截取 String.substring(int index) 返回从下标index开始到结尾的字符串
* String.substring(int a,int b) 返回从下标a开始到下标b-1结尾的字符串
**/

import java.sql.SQLOutput;

public class StringDemo {
    public static void main(String[] args) {
        String str  =   "Begin challeging your own assumptions. Your assumptions are your windows on the world. Scrub them off every once in a while, or the light won’t come in";
        //获取字符串长度
        System.out.println(str.length());
        //区分大小写比较两个字符串是否相等
        System.out.println(str.equals("hello"));
        //不区分大小写比较两个字符串是否相等
        System.out.println(str.equalsIgnoreCase("hello"));
        //获取第一个字符的值
        System.out.println(str.charAt(0));
        //获取字符e的下标
        System.out.println(str.indexOf("e"));
        //从指定的下标开始查找字符,并返回字符在字符串的下标,未找到返回-1
        System.out.println(str.indexOf('c',3));
        //截取从指定下标开始到结尾的字符串
        System.out.println(str.substring(10));
        //截取从指定开始下标到指定结束下标-1的字符串
        System.out.println(str.substring(10,15));
    }
}

猜你喜欢

转载自www.cnblogs.com/vxiao2/p/11488241.html