Java小专题之String类
来源:互联网 发布:网络综合布线报价单 编辑:程序博客网 时间:2024/05/20 01:09
- 1-字符串内部
- 1-1 字符串字符
- 1-2 字符串索引
- 2-字符串本身
- 2-1 字符串属性
- 2-2 字符串判断
- 3-字符串操作
- 3-1 字符串拼接
- 3-2 字符串格式化
- 3-3 替换字符串
- 3-4 截取和拆分字符串
- 3-5 正则表达式操作字符串
- 3-5-1 正则表达式格式化字符串
- 3-5-2 正则表达式替换字符串
- 3-5-3 正则表达式拆分字符串
- 4-字符串比较
- 5-字符串转换
- 5-1 将字符串转换成其他类型
- 5-2 将其他数据类型转换成字符串
- 1-字符串内部
说明:红色标注为应了解部分,不标注可忽略!
1-字符串内部
1-1 字符串字符
- char charAt(int index)
- 返回指定索引处的 char 值。
- int codePointAt(int index)
- 返回指定索引处的字符(Unicode 代码点)。
- int codePointBefore(int index)
- 返回指定索引之前的字符(Unicode 代码点)。
- int codePointCount(int beginIndex, int endIndex)
- 返回此 String 的指定文本范围中的 Unicode 代码点数。
- 这个方法就是计算unicode字符数量的。原因是:一个java的char字符并不完全等于一个unicode字符。
char采用UCS-2编码。这是一种淘汰的UTF-16编码,最多65536种形态。
unicode拥有11万字符的形态。
java堆后来新增的unicode字符用2个char拼出1个unicode字符。
导致String中char的数量不等于unicode字符的数量。
public class Demo{ public static void main(String[] args){ String s="helloworld"; char c=s.charAt(4); int i=s.codePointAt(4); int i2=s.codePointBefore(4); int i3=s.codePointCount(0,4); System.out.println("c:"+c);//c:o System.out.println("i:"+i);//i:111 System.out.println("i2:"+i2);//i:108 System.out.println("i3:"+i3);//i3:4 }}
1-2 字符串索引
- int indexOf(int ch)
- 返回指定字符在此字符串中第一次出现处的索引。
- int indexOf(int ch, int fromIndex)
- 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
- int indexOf(String str)
- 返回指定子字符串在此字符串中第一次出现处的索引。
- int indexOf(String str, int fromIndex)
- 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
- int lastIndexOf(int ch)
- 返回指定字符在此字符串中最后一次出现处的索引。
- int lastIndexOf(int ch, int fromIndex)
- 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
- int lastIndexOf(String str)
- 返回指定子字符串在此字符串中最右边出现处的索引。
- int lastIndexOf(String str, int fromIndex)
- 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
- int offsetByCodePoints(int index, int codePointOffset)
- 返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
public class Demo{ public static void main(String[] args){ String s="hello,world"; int i1=s.indexOf('w'); System.out.println("i1:"+i1);//i:6 int i2=s.indexOf('l', 1);//从1号索引开始搜索第一次出现"1"的位置。 System.out.println("i2:"+i2);//i2:2 int i3=s.indexOf("world"); System.out.println("i3:"+i3);//i3:6 int i4=s.indexOf("world",7); System.out.println("i4:"+i4);//i4:-1 -1表示不存在。 int i5=s.lastIndexOf('l'); System.out.println("i5:"+i5);//i5:9 最后出现的位置的索引 int i6=s.lastIndexOf("o", 5); System.out.println("i6:"+i6);//i6:4 索引为5的位置前面最后一次出现o的位置是索引为4的位置。 int i7=s.lastIndexOf("wo"); System.out.println("i7:"+i7);//i7:6 int i8=s.lastIndexOf("wo",7); System.out.println("i8:"+i8);//i8:6 索引为7之前,最后一次出现"wo"这个字符串的开始位置索引。 int i9=s.offsetByCodePoints(3, 4); System.out.println("i9:"+i9);//i9:7 从3开始偏移4个代码点,3+4即可!不知道有什么卵用! }}
2-字符串本身
2-1 字符串属性
- int length()
- 返回此字符串的长度。
- int hashCode()
- 返回此字符串的哈希码。
- boolean isEmpty()
- 当且仅当 length() 为 0 时返回 true。
- String intern()
- 返回字符串对象的规范化表示形式。
public class Demo{ public static void main(String[] args){ String s="hello,world"; System.out.println(s.length()); System.out.println(s.hashCode()); System.out.println(s.intern()); boolean b=s.isEmpty(); System.out.println(b); }}
2-2 字符串判断
- boolean regionMatches(int toffset, String other, int ooffset, int len)
- 测试两个字符串区域是否相等。
- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- 测试两个字符串区域是否相等。
- boolean contains(CharSequence s)
- 当且仅当此字符串包含指定的 char 值序列时,返回 true。
- boolean startsWith(String prefix)
- 测试此字符串是否以指定的前缀开始。
- boolean startsWith(String prefix, int toffset)
- 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
- String从指定位置开始,是否是指定前缀
- boolean endsWith(String suffix)
- 测试此字符串是否以指定的后缀结束。
public class Demo { public static void main(String[] args){ String s="hello,world"; boolean b0=s.regionMatches(0,"hello",0,5);//区分大小写,0是s中比较起始位置,第二个0是hello中比较起始位置,5是比较长度。 boolean b1=s.regionMatches(true,0,"HELLO",0,5);//和上面的方法一样,是不区分大小写。 boolean b2=s.contains("world"); boolean b3=s.startsWith("he"); boolean b4=s.startsWith("ll", 2); boolean b5=s.endsWith("ld"); System.out.println(b0);//true System.out.println(b1);//true System.out.println(b2);//true System.out.println(b3);//true System.out.println(b4);//true System.out.println(b5);//true }}
3-字符串操作
3-1 字符串拼接
- String concat(String str)
- 将指定字符串连接到此字符串的结尾。
public class Demo{ public static void main(String[] args){ String s1="hello"; String s2="world"; String s=s1.concat(s2); System.out.println(s); }}
3-2 字符串格式化
- static String format(Locale l, String format, Object… args)
- 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
- static String format(String format, Object… args)
- 使用指定的格式字符串和参数返回一个格式化字符串。
- String trim()
- 返回字符串的副本,忽略前导空白和尾部空白。
- 将字符串前后空白去掉
- String toUpperCase()
- 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
- String toUpperCase(Locale locale)
- 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
- String toLowerCase()
- 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
- String toLowerCase(Locale locale)
- 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
public class Demo{ public static void main(String[] args){ String s=" hello,world! "; System.out.println(s.trim());//hello,world! System.out.println(s.toUpperCase());// HELLO,WORLD! System.out.println(s.toLowerCase());// hello,world! }}
3-3 替换字符串
- String replace(char oldChar, char newChar)
- 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
- String replace(CharSequence target, CharSequence replacement)
- 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
public class Demo{ public static void main(String[] args){ String s="hello,world!"; s=s.replace("hello","!!!!!"); System.out.println(s);//!!!!!,world! }}
3-4 截取和拆分字符串
- String substring(int beginIndex)
- 返回一个新的字符串,它是此字符串的一个子字符串。
- 截取字符串,从指定索引到最后
- String substring(int beginIndex, int endIndex)
- 返回一个新字符串,它是此字符串的一个子字符串。
- 截取字符串,包含beginIndex,不含endIndex
- CharSequence subSequence(int beginIndex, int endIndex)
- 返回一个新的字符序列,它是此序列的一个子序列。
public class Demo{ public static void main(String[] args){ String s="hello,world!"; s=s.substring(3); System.out.println(s);//lo,world! s=s.substring(0,3); System.out.println(s);//lo,包含begin,不包含end }}
3-5 正则表达式操作字符串
3-5-1 正则表达式格式化字符串
- boolean matches(String regex)
- 告知此字符串是否匹配给定的正则表达式。
3-5-2 正则表达式替换字符串
- String replaceAll(String regex, String replacement)
- 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
- String replaceFirst(String regex, String replacement)
- 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
3-5-3 正则表达式拆分字符串
- String[] split(String regex)
- 根据给定正则表达式的匹配拆分此字符串。
- String[] split(String regex, int limit)
- 根据匹配给定的正则表达式来拆分此字符串。
4-字符串比较
- int compareTo(String anotherString)
- 按字典顺序比较两个字符串。
- int compareToIgnoreCase(String str)
- 按字典顺序比较两个字符串,不考虑大小写。
- boolean equals(Object anObject)
- 将此字符串与指定的对象比较。
- boolean equalsIgnoreCase(String anotherString)
- 将此 String 与另一个 String 比较,不考虑大小写。
- boolean contentEquals(CharSequence cs)
- 将此字符串与指定的 CharSequence 比较。
- contentEquals()和equals()都能比较string序列内容,区别是equals要求对方必须是string, contentEquals则没这个硬性要求,可以是CharSequence或者其他子类对象。
- boolean contentEquals(StringBuffer sb)
- 将此字符串与指定的 StringBuffer 比较。
public class Demo{ public static void main(String[] args){ String s1="aaaaaaaaaa"; String s2="aaaaaaAAAA"; int i1=s1.compareTo(s2); int i2=s1.compareToIgnoreCase(s2); System.out.println(i1);//32 System.out.println(i2);//0 System.out.println(s1.equals(s2));//false 区分大小写 System.out.println(s1.equalsIgnoreCase(s2));//true 不区分大小写 System.out.println(s1.contentEquals(s2));//false }}
5-字符串转换
5-1 将字符串转换成其他类型
- void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- 将字符从此字符串复制到目标字符数组。
- String toString()
- 返回此对象本身(它已经是一个字符串!)。
- byte[] getBytes(String charsetName)
- 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- byte[] getBytes()
- 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- byte[] getBytes(Charset charset)
- 使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
- char[] toCharArray()
- 将此字符串转换为一个新的字符数组。
public static void main(String[] args){ String s="hello,world"; char[] c={'-','-','-','-','-','-','-','-','-','-','-',}; s.getChars(4,10,c,2);//包含开始的4,不包含结束的10 for(int i=0;i<c.length;i++){ System.out.print(c[i]); }//--o,worl--- System.out.println(); char[] c2=s.toCharArray(); for(int i=0;i<c2.length;i++){ System.out.print(c2[i]); }//hello,world }
5-2 将其他数据类型转换成字符串
- void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
- 已过时。 该方法无法将字符正确转换为字节。从 JDK 1.1 起,完成该转换的首选方法是通过 getBytes() 方法,该方法使用平台的默认字符集。
- static String copyValueOf(char[] data)
- 返回指定数组中表示该字符序列的 String。
- static String copyValueOf(char[] data, int offset, int count)
- 返回指定数组中表示该字符序列的 String。
- static String valueOf(boolean b)
- 返回 boolean 参数的字符串表示形式。
- static String valueOf(char c)
- 返回 char 参数的字符串表示形式。
- static String valueOf(char[] data)
- 返回 char 数组参数的字符串表示形式。
- static String valueOf(char[] data, int offset, int count)
- 返回 char 数组参数的特定子数组的字符串表示形式。
- static String valueOf(double d)
- 返回 double 参数的字符串表示形式。
- static String valueOf(float f)
- 返回 float 参数的字符串表示形式。
- static String valueOf(int i)
- 返回 int 参数的字符串表示形式。
- static String valueOf(long l)
- 返回 long 参数的字符串表示形式。
- static String valueOf(Object obj)
- 返回 Object 参数的字符串表示形式。
public class Demo{ public static void main(String[] args){ String s; s=String.valueOf(new Object()); System.out.println(s);//java.lang.Object@60e128 }}
0 0
- Java小专题之String类
- Java小专题之常用类库
- java小专题之API
- Java小专题之集合
- Java小专题之IO
- Java小专题之小程序
- Java小专题之JDK结构详解
- 五:Java之Vector类专题
- Java专题之IO
- Java专题之集合
- Java之String类
- Java之String类
- java类之String
- Java之String类
- Java学习--String类小细节
- java小知识 接口 String类
- JAVA STRING 小知识
- Java String 小练习
- 开源跳板机-堡垒机jumpserver安装
- python 解决No module named 'MySQLdb'的问题
- nodejs返回html代码 引入路径问题+静态文件问题
- iOS应用架构谈 网络层设计方案
- 【架构师之路】-【01设计模式】-05设计原则之接口分离原则
- Java小专题之String类
- 探索react-redux的小秘密
- InputStream与OutputStream的使用例子
- [拉格朗日插值法]拉格朗日插值法小结
- AngularJS的核心机制--脏检查
- 关于oracle连“System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本”
- Android中如何给layout资源文件分类 在layout下创建子文件夹
- 人工智能自动代码生成3---数据库获取代码实现1
- L1-030. 一帮一