字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。
String str1="hello world";
String str2=new String("hello world");
运行结果:
1.注意::String 类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了
示例:int length=str1.length();
运行结果:
String类中提供链接两个字符串的方法:string1.concat(string2);
示例:String str3=new String("how "); String str4=new String("are you"); System.out.println(str3.concat(str4));
运行结果:
示例:String str="Java is so easy"; char ch=str.charAt(3); System.out.println(ch);
方法:str.getChars(indexBegin,indexEnd,array,arrayBegin);
indexBegin | 需要复制的字符串的开始索引 |
indexEnd | 需要复制的字符串的结束索引,indexEnd-1 |
array | 定义的char型数组的数组名 |
arrayBegin | 数组array开始存储的位置索引号 |
示例:String str="hello world"; char[] array=new char[90]; str.getChars(0,5,array,0); System.out.println(array);
运行结果:
示例:String str1=new String("HOW"); String str2=new String("how"); int res=str1.compareTo(str2); System.out.println("res---->"+res);
输出结果:
输出结果说明:若该字符串的Unicode值<参数字符串的Unicode值,结果返回一负整数;若若该字符串的Unicode值=参数字符串的Unicode值,结果返回0;若该字符串的Unicode值>参数字符串的Unicode值,结果返回一正整数。
示例:int res=str1.compareToIgnoreCase(str2); System.out.println("res---->"+res);
运行结果:
示例:String str1=new String("HOW"); String str2=new String("how"); boolean res=str1.equals(str2); System.out.println("res--->"+res);
输出结果:
示例:String str1=new String("HOW"); String str2=new String("how"); boolean res=str1.equalsIgnoreCase(str2); System.out.println("res--->"+res);
输出结果:
方法一:char charAt(int index)
作用 : 返回指定索引处的 char 值。
方法二:int compareTo(Object o)
作用 : 把这个字符串和另一个对象比较。
方法三:int compareTo(String str)
作用 : 按字典顺序比较两个字符串。
方法四:int compareToIgnoreCase(String str)
作用 : 按字典顺序比较两个字符串,不考虑大小写。
方法五:String concat(String str)
作用 : 将指定字符串连接到此字符串的结尾。
方法六:boolean contentEquals(StringBuffer sb)
作用 : 当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
方法七:static String copyValueOf(char[] data)
作用 : 返回指定数组中表示该字符序列的 String。
方法八:static String copyValueOf(char[] data,int offset,int count)
作用 : 返回指定数组中表示该字符序列的 String。
方法九:boolean endWith(String suffix)
作用 : 测试此字符串是否以指定的后缀结束。
方法十:boolean equals(Object anObject)
作用 : 将此字符串与指定的对象比较。
方法十一:int hashCode()
作用 : 返回此字符串的哈希码。
方法十二:int indexOf(int ch)
作用 : 返回指定字符在此字符串中第一次出现处的索引。
方法十三:int indexOf(int ch,int fromIndex)
作用 : 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
方法十四:int indexOf(String str)
作用 : 返回指定子字符串在此字符串中第一次出现处的索引。
方法十五:String replace(char oldChar,char newChar)
作用 : 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
方法十六:String replaceAll(String regex,String replacement)
作用 : 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
方式一:String result = str.substring(index);
方式二:String result = str.substring(beginIndex,EndIndex);//实际索引号[beginIndex,EndIndex-1]
输出结果:截取出范围内的字符串
方式一:String strArray[] = str.split(正则表达式);// 拆分的结果保存到字符串数组中
方式二:String strArray[] = str.split(正则表达式,limit);
方式:String result = str1.concat(str2); //将str1和str2合并
方式:String result = str.toLowerCase();
方式:String result = str.toUpperCase();
[1] public String() 空构造
[2] public String (byte [] bytes ) 将字节数组转换为字符串。
[3] public String(byte [] bytes, int index, int length) 把字节数组的一部分转成字符串(从index开始,长度为length)
[4] public String(char [] value ) 把字符数组转成字符串
[5] public String(chat [] value ,int index, int count) 把字符数组的一部分转成字符串
[6] public string(String original)把字符串常量转换成字符串
原文:https://www.cnblogs.com/hunanloudi/p/14981692.html