您好,欢迎访问一九零五行业门户网

关于java中的常用类——String的详细介绍

概述
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的详细介绍的详细内容。
其它类似信息

推荐信息