概述
java.lang.String
类代表字符串。Java顺序中一切的字符串笔墨(比方"abc")都能够被看做是完成此类的实例
String 中包含用于搜检各个字符串的要领,比方用于比较字符串,搜刮字符串,提取子字符串以及建立具有翻译为大写或小写的一切字符的字符串的副本。
特性
1.字符串稳定:字符串的值在建立后不能被变动
String s1 = "abc"; s1 += "d"; System.out.println(s1); // "abcd" // 内存中有"abc","abcd"两个对象,s1从指向"abc",转变指向,指向了"abcd"。
2.由于String对象是不可变的,所以它们能够被同享
String s1 = "abc"; String s2 = "abc"; // 内存中只要一个"abc"对象被建立,同时被s1和s2同享。
3."abc"等效于 char[] data = {'a','b','c'}
在线进修视频分享:java视频
运用步骤
检察类
java.lang.String
此类不需要导入。
检察组织要领
public String():
初始化新建立的 String对象,以使其示意空字符序列。
public String(char[] value)
: 经由过程当前参数中的字符数组来组织新的String。
public String(byte[] bytes)
: 经由过程运用平台的默许字符集解码当前参数中的字节数组来组织新的String。
组织举例,代码以下:
经常使用要领
推断功用的要领
public boolean equals (Object anObject)
:将此字符串与指定对象举行比较。
public boolean equalsIgnoreCase (String anotherString)
: 将此字符串与指定对象举行比较,疏忽大小写
要领演示,代码以下:
Object 是” 对象”的意义,也是一种援用范例。作为参数范例,示意恣意对象都能够通报到要领中。
猎取功用的要领
public int length ()
返回此字符串的长度。
public String concat (String str)
: 将指定的字符勾通接到该字符串的末端。
public char charAt (int index)
: 返回指定索引处的 char值。
public int indexOf (String str):
该字符串第一次涌现的索引位置
public String substring (int beginIndex)
: 返回一个子字符串,从begin Index入手下手截取字符串到字符串末端
public String substring (int beginIndex, int endIndex)
: 返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
要领演示,代码以下:
public class String_Demo02 { public static void main(String[] args) { //建立字符串对象 String s = "helloworld"; // int length():猎取字符串的长度,实在也就是字符个数 // System.out.println(s.length()); // System.out.println("‐‐‐‐‐‐‐‐"); // String concat (String str):将将指定的字符勾通接到该字符串的末端. // String s = "helloworld"; String s2 = s.concat("**hello itheima"); // char charAt(int index):猎取指定索引处的字符 // System.out.println(s.charAt(0)); // System.out.println(s.charAt(1)); // System.out.println("‐‐‐‐‐‐‐‐"); // int indexOf(String str):猎取str在字符串对象中第一次涌现的索引,没有返回‐1 // System.out.println(s.indexOf("l")); // System.out.println(s.indexOf("owo")); // System.out.println(s.indexOf("ak")); System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start):从start入手下手截取字符串到字符串末端 // System.out.println(s.substring(0)); // System.out.println(s.substring(5)); // System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start,int end):从start到end截取字符串。含start,不含end。 // System.out.println(s.substring(0, s.length())); // System.out.println(s.substring(3,8)); } }
public char[] toCharArray ()
: 将此字符串转换为新的字符串数组
public byte[] getBytes ()
: 运用平台默许的字符集将该String编码转换为新的字节数组
public String replace (CharSequence target, CharSequence replacement)
: 将与target婚配的字符串运用replacement字符串替代。
要领演示,代码以下:
public class String_Demo03 { public static void main(String[] args) { //建立字符串对象String s = "abcde"; // char[] toCharArray():把字符串转换为字符数组 // char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // byte[] getBytes ():把字符串转换为字节数组 // byte[] bytes = s.getBytes(); for (int x = 0; x < bytes.length; x++) { System.out.println(bytes[x]); } System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); // 替代字母it为大写IT // String replace = str.replace("it", "IT"); // System.out.println(replace); // ITcast ITheima System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); } }
CharSequence 是一个接口,也是一种援用范例。作为参数范例,能够把String对象通报到要领中。
支解功用的要领
public String[] split(String regex)
将此字符串根据给定的regex(划定规矩)拆分为字符串数组。
要领演示,代码以下:
String类的演习
拼接字符串
定义一个要领,把数组{1,2,3}根据指定个花样拼接成一个字符串。花样参照以下:
public class StringTest1 { public static void main(String[] args) { //定义一个int范例的数组 // int[] arr = {1, 2, 3}; //挪用要领 String s = arrayToString(arr); //输出效果System.out.println("s:" + s); } /* *写要领完成把数组中的元素根据指定的花样拼接成一个字符串 *两个明白: *返回值范例:String *参数列表:int[] arr */ public static String arrayToString(int[] arr) { // 建立字符串s String s = new String("["); // 遍历数组,并拼接字符串 for (int x = 0; x < arr.length; x++) { if (x == arr.length - 1) { s = s.concat(arr[x] + "]"); } else { s = s.concat(arr[x] + "#"); } } return s; } }
统计字符个数
键盘录入一个字符,统计字符串中大小写字母及数字字符个数
相干文章教程引荐:java入门
以上就是关于java中的经常使用类——String的细致引见的细致内容,更多请关注ki4网别的相干文章!