Java基本语法

String的基本用法


package com.test.string;

public class StringDemo {
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        String str = "xhl";
        
        // 返回字符在此字符串中第一次出现处的索引
        int indexOf = str.indexOf("a");
        System.out.println("a第一次出现处的索引"+indexOf);
        
        // 返回指定字符在此字符串中最后一次出现的索引
        int lastIndexOf = str.lastIndexOf("i");
        System.out.println("i最后一次出现的索引"+lastIndexOf);
        
        // 返回索引处的char值
        char charAt = str.charAt(3);
        System.out.println("下标索引3的char值"+charAt);
        
        // 判断是否以指定的字符结尾
        boolean endsWith = str.endsWith("ui");
        System.out.println("判断是否以ui字符结尾"+endsWith);
        
        // 判断此字符和指定字符是否相等
        boolean equals = str.equals("cenxiaodan");
        System.out.println("判断xhl和xdc是否相等"+equals);
        
        // 当且仅当字符串长度为0时返回true
        boolean empty = str.isEmpty();
        System.out.println("判断字符串长度"+empty);
        
        // 判断此字符串是否以指定的字符串开始
        boolean startsWith = str.startsWith("lin");
        System.out.println("判断字符串是否以lin开始"+startsWith);
        
        // 判断是否字符串中是否包含特定的字符序列
        boolean contains = str.contains("xiao");
        System.out.println("判断是否字符串中是否包含xiao"+contains);
        
        // 把String中的所有字符转化为小写
        String lowerCase = str.toLowerCase();
        System.out.println("所有字符转化为小写"+lowerCase);
        
        // 把String中所有的字符转化为大写
        String upperCase = str.toUpperCase();
        System.out.println("所有的字符转化为大写"+upperCase);
        
        // 删除头尾空白符的字符串
        String valueOf = String.valueOf(str);
        System.out.println("删除头尾空白符的字符串"+valueOf);
        
        // 将此字符串转化为数组
        char[] charArray = str.toCharArray();
        System.out.print("将此字符串转化为数组:");
        for (int i = 0; i < charArray.length; i++) {
            System.out.print(charArray[i]);
        }
        
        System.out.println();
        
        // 替换字符串中的指定字符
        String replace = str.replace("hui", "dan");
        System.out.println("替换字符串中的hui"+replace);
        
        // 根据参数对字符串进行分割
        String[] split = str.split("xiao");
        for (int i = 0; i < split.length; i++) {
            System.out.println("根据xiao对字符串进行分割"+split[i]);
        }
        
        // 返回一个新字符串,从开始索引到最后索引的所有字符
        String substring = str.substring(3, str.length()-1);
        System.out.println("返回一个新字符串,从索引下标3到最后索引的所有字符"+substring);
        
        // 返回一个新字符串,从开始索引到最后
        String substring2 = str.substring(3);
        System.out.println("返回一个新字符串,从索引下标3到最后"+substring2);
        
        // 返回一个新字符串,去掉首尾空格
        String trim = str.trim();
        System.out.println("去掉首尾空格"+trim);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 三天基本学会Java,挑战不可能 Java以其简易性而闻名于世 , 然而市场上大量几百页的厚重书籍会打消初学者的兴...
    无状态Rio阅读 26,117评论 7 29
  • 最近比较烦,点背的时候,喝凉水都是塞牙的,其实我也很想赶项目,把后台接口写好,但是我还是姥姥实实的把基础语法搞个大...
    _信仰zmh阅读 1,358评论 1 8
  • 基本语法: 大小写敏感:大小写敏感类名:类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写...
    傻傻笨笨宝宝阅读 214评论 0 1
  • 注释:单行注释://[用一行注释对代码进行解释说明] 多行注释:/**/ [用多行注释对代码进行解释说明(注释一...
    征程_Journey阅读 242评论 0 0
  • 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作。 类、对象、方法和实例变量的概...
    莫要戏言阅读 176评论 0 0