栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

String 常用的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

String 常用的方法

String API总结
int hashCode() 返回此字符串的哈希码。
boolean equals(Object anObject) 将此字符串与指定的对象比较,比较的是重写后的串的具体内容
String toString() 返回此对象本身(它已经是一个字符串!)。
int length() 返回此字符串的长度。
String toUpperCase() 所有字符都转换为大写。
String toLowerCase() 所有字符都转换为小写
boolean startsWith(String prefix) 测试此字符串是否以指定的元素开头。
boolean endsWith(String suffix) 测试此字符串是否以指定的字符串结束。
char charAt(int index) 返回指定索引/下标处的 char 值/字符
int indexOf(String str) 返回指定字符在此字符串中第一次出现处的索引。
int lastIndexOf(String str) 返回指定字符在此字符串中最后一次出现处的索引。
String concat(String str) 将指定字符串连接/拼接到此字符串的结尾,注意:不会改变原串
String[] split(String regex) 根据给定元素来分隔此字符串。
String trim() 返回去除首尾空格的字符串
byte[] getBytes() 把字符串存储到一个新的 byte 数组中
String substring(int beginIndex) 返回一个新子串,从指定下标处开始,包含指定下标
String substring(int beginIndex, int endIndex) 返回一个新子串,从执定下标开始,到结束下标为止,但不包含结束下标
static String valueOf(int i) 把int转成Stre
eg: 
 public static void main(String[] args) {
        //1.1创建字符串的方式1--推中常量
        String s1="abc";
        //1.2创建字符串方式2--堆中,new一次,创建一个对象
        char[] c={'a','b','c'};
        String  s2=new String(c);
        System.out.println(s1);
        System.out.println(s2);
        //2.测试常用的方法
        System.out.println(s1.hashCode());//96354.显示哈希码值
        System.out.println(s2.hashCode());
        System.out.println(s1.equals(s2));//true,equals
        System.out.println(s1);
        System.out.println(s1.length());//测试字符长度
        System.out.println(s1.toUpperCase());//全部大写
        System.out.println(s1.toLowerCase());//全部小写
        System.out.println(s1.startsWith("a"));//判断以什么开头 true
        System.out.println(s1.endsWith("a"));//判断什么结尾
        System.out.println(s1.charAt(2));//获取字符的位置从0开始计算
        String  s3="abcdefg";
        System.out.println(s3.indexOf('b'));//第一次出现的下角标
        System.out.println(s3.lastIndexOf("g"));//最后一次出现的下角标
        System.out.println(s3.concat("666"));//拼接字符,但是不能改变s3的值
        String  s4=s3.concat("666");
        System.out.println(s4);
        String s5="      HHH HHH  HHH";
        System.out.println(s5.trim());//删除空白部分
        String  s6="abcdefg111";
        System.out.println(s6.substring(3));//从指定下标截取字符串【3,结束)
        System.out.println(s6.substring(3,6));//从指定下标截取【3-6)字符串不含结尾
        String  s7="abcdefg";
        String [] f=s7.split("f");//以指定的字符f分割字符串s7
        System.out.println(Arrays.toString(f));//要是使用数组工具类打印数组的具体元素,否则堆是地址值
        System.out.println(String.valueOf(10));//10
        System.out.println(String.valueOf(10)+10);//10
        System.out.println(20+10);
        System.out.println("20"+10);
        byte[] bytes=s2.getBytes();
        System.out.println(Arrays.toString(bytes));

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337738.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号