本文主要是介绍1-29 核心类库(五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一String类
1.作用:存储多个连续字符的容器,值定义在" "中,默认值是null
2.赋值方式
①直接赋值: String 变量名="值";
②构造器赋值:String 对象名=new String("值");
public class StringFuZhiTest {public static void main(String[] args) {String s1="abc";String s2="abc";String s3=new String("abc");System.out.println(s1==s2);System.out.println(s1==s3);/***false的原因: ==比较的是就是栈内存的* 构造器的对象是在堆里面的存储,所以 s1s3的地址不一样**/System.out.println(s1.equals(s3));System.out.println("abc"==s1);}
}
final修饰 表示:String类不能被继承 后续出现的
StringBuilder StringBuffer 和String没有继承关系,是在String基础上 进行功能拓展
String 相当于char[] 这也是String不能转换 char Character
二 String类常用的API
1.转换.
①String<-->byte[]
import java.util.Arrays;public class StringAPITest1 {public static void main(String[] args) {String str="金风玉露一相逢,便胜却,人间无数";byte[] bbuf=str.getBytes();System.out.println("length"+bbuf.length);System.out.println(Arrays.toString(bbuf));/*** 如果是有中文, 一个中文 对应三个字节* 一个符号对应一个字节* GBK编码的话 一个汉字对应两个字节*//*** 将指定的字节数组 转换字符串*/String byteToString=new String(bbuf,0,bbuf.length);System.out.println(byteToString);/** 一般转换的时候都是从头转换的 不漏字* 如果转换的时候 长度不够* 用?代替*/}
}
②String<-->char[](常用)
import java.util.Arrays;public class StringAPI2 {public static void main(String[] args) {String str="安能催眉折腰侍权贵";/*** 将指定String 转换为char[]*/char[] chs=str.toCharArray();System.out.println(Arrays.toString(chs));/*** 将指定的字节数组转换为String类型* 第一个参数:转换的字符数组* 第二个参数:从哪开始转换* 第三个参数:转换多长*/String charToString=new String(chs,5,3);System.out.println(charToString);}
}
2.修改.
①替换指定的字符 和 去除指定字符的首尾空格
public class StringAPI3 {public static void main(String[] args) {String str="I LLLLLove 中国";/*** public String replace(char old,char newChar)* 使用新字符 替换原有的字符(字符存在)* 全换所有的*/System.out.println("将l替换为K"+str.replace('L','k'));String str1=" 去掉 首尾 空格 ";System.out.println("去掉之前"+str1);/*** public String trim()* 去掉指定String首尾空格*/System.out.println("去掉后:"+str1.trim());}
}
3.查询.
① 获取指定位置上的元素
②获取指定元素在String中第一次或最后一次出现的位置
public class StringAPI4 {public static void main(String[] args) {String str="abcdfegasdasdsd";/*** public char charAt(int index)* 获取指定位置上的字符*/System.out.println("3位置上的是"+str.charAt(3));/*** public int indexOf(int ch)* public int indexOf(String son)* int ch----表示用户既可以 传递char 类型的数据* 也可以传递 char 对应的ASCII 码的值*/System.out.println("a在字符串中第一次出现的位置是"+str.indexOf('a'));System.out.println("a在字符串中第一次出现的位置是"+str.indexOf(97));System.out.println("ab在字符串中第一次出现的位置是"+str.indexOf("ab"));/**** public int indexOf(int ch,int beginIndex)* public int indexOf(String son,int beginIndex)* 从指定的位置开始向后去查元素 第一个出现的位置*/System.out.println("a在字符串中第一次出现的位置是"+str.indexOf('a',3));System.out.println("a在字符串中第一次出现的位置是"+str.indexOf(97,3));System.out.println("ab在字符串中第一次出现的位置是"+str.indexOf("ab",3));/**** public int lastIndexOf(int ch,int beginIndex)* public int lastIndexOf(String son,int beginIndex)* 从指定的位置开始向前去查元素 最后出现的位置*/System.out.println("从第三个的位置开始向前查a是"+str.lastIndexOf('a',3));System.out.println(" 是"+str.lastIndexOf(97,3));}
}
(3)截取指定的字符串
import java.util.UUID;public class StringAPI5 {public static void main(String[] args) {//UUID 来截取验证码UUID uuid=UUID.randomUUID();System.out.println("uuid"+uuid);String s=uuid.toString();/*** public String subString(int beginIndex)* 从指定位置开始截取到字符串末尾* public String subString(int beginIndex,int endIndex)* 从指定位置开始 到指定位置结束(不包含结束的位置)*/System.out.println(s.substring(0,4));System.out.println(s.substring(5));}
}
这篇关于1-29 核心类库(五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!