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

JAVA--String类的方法

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

JAVA--String类的方法

1.string方法如下

获取字符串长度 int length();
String s ="zxcvbnm";//7个字母
   System.out.println(s.length());//执行结果:7
获取特定位置的字符 char charAt(int  index);
String s ="zxcvbnm";
   System.out.println(s.charAt(4));//执行结果为:b
获取特定字符的下标 int indexOf(String str);
String s ="zxcvbnm";
   System.out.println(s.indexOf("m"));//执行结果为:6
获取最后一个字符位置 int lastlndexOf(int ch);
String s ="zxcvbnm";
System.out.println(s.lastIndexOf('m'));
返回值是布尔类型的数据

boolean endWith(String str); 是否以指定的字符或者子字符串结尾

String a ="aqwert";
System.out.println(a.endsWith("t"));//true
System.out.println(a.endsWith("r"));//false
boolean isEmpty();判断是否为空
String a ="aqwert";
System.out.println("a".isEmpty());//true
System.out.println("".isEmpty());//false
boolean conatins(String str); 是否包含子字符串
System.out.println("zxc".contains("z"));//true
System.out.println("zxc".contains("a"));//false
boolean equals(object obj); 判断字符串内容是否相等

System.out.println("abc".equals("ABc"));//false

boolean equalsIgnoreCase();忽略大小写 判断两个字符串是否相等

System.out.println("abc".equalsIgnoreCase("ABc"));//true

将字符数组转为字符串

直接使用String类的构造方法

new String (char[] chs);

char[] chs = {'中', '午', '吃', '黄', '焖', '鸡'};
        String str1 = new String(chs);
        System.out.println(str1);//执行结果为:中午吃黄焖鸡

static valueOf(char[] chs);【重点】
char[] chs = {'中', '午', '吃', '黄', '焖', '鸡'};
String str3 = String.valueOf(chs);
System.out.println(str3);//执行结果为:中午吃黄焖鸡
String b1 = String.valueOf(true);
System.out.println(b1);//true

String i1 = String.valueOf(12);
        System.out.println(i1);//12
        String d1 = String.valueOf(12.4589494949494949499);
        System.out.println(d1);//12.4589494949494949499

将字符串转为字符数组 char[] toCharArray();

//字符串转数组
   //八大基本数据类型的数组打印的都是十六进制内存地址  除了char数组

char[] arr = "abcd".toCharArray();
        System.out.println(arr);//abcd
        int[] arr1 = {1,2,4};
        System.out.println(arr1);//内存地址

重点: String replace(char oldChar, char newChar); 字符在字符串中的替换

System.out.println("abcdef".replace('c', '人'));//ab人def

String replaceAll(String regex, String original);字符串在字符串中的替换

System.out.println("天天向上".replace('上', '下'));//天天向下

String[] split(String regex); 以regex来进行字符串的切割

 

String subString(int beginIndex); 截取字符串的一部分

System.out.println("abcdef".substring(2));//要头不要尾,执行结果为:cdef

String subString(int beginIndex, int endIndex); 截取字符串的一部分

System.out.println("abcdef".substring(2,4));//cd

String toUpperCase();将小写字母转换为大写字母
System.out.println("abCD".toUpperCase());//ABCD
String toLowerCase();将大写转为小写字母
System.out.println("ABFRgh".toLowerCase());//abfrgh
String trim();去除字符串中首尾的空格

System.out.println("  abcd  fgh ".trim());//abcd  dgh

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

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

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