String类
- 字符串是一个特殊的对象。
- 字符串一旦初始化就不可以被改变。
String s="abc";
特点:
String构造函数
主要几个String构造函数
String(byte[] bytes){}
1、获取字符串中字符的个数(长度)
int length();
2、根据位置获取字符
char charAt(int index);
3、根据字符(字符串)获取字符串中第一次出现的位置。从前往后查
1. int indexOf(int ch); 2. int indexOf(int ch,int fromIndex);
4、根据字符(字符串)获取字符串中第一次出现的位置。从后往前查
1. int lastIndexOf(int ch); 2. int lastIndexOf(int ch,int fromIndex); 3. int lastIndexOf(String str); 4. int lastIndexOf(String str,int fromIndex);
5、获取字符串中一部分字符串,子串
String substring(int beginIndex, int endIndex);
1、将字符串转换成字符串数组(切割)
String[] split(String regex);
2、将字符串转换成字符(char)数组
char[] toCharArray();
3、将字符串转换成字节数组
byte[] getBytes();
4、将字符串中的字母转换成大小写
String toUpperCase();
5、将字符串中的内容进行替换
6、将字符串两端空格去除
String trim();
7、将字符串进行连接
String concat(String str);
1、两个字符串内容是否相同
boolean equals(Object obj); boolean equalsIgnoreCase(String str);
2、字符串中是否包含指定字符串
boolean contains(String str);
3、字符串是否以指定字符串开头,或结尾
boolean startsWith(String str);
按字典顺序比较两个字符串
int compareTo(String anotherString)
字符串对象的规范化表示
String intern();
事例:
String t=new String("abc");
Original: https://www.cnblogs.com/wdsjg/p/13639526.html
Author: 所有的都不在以往
Title: Java基础 String
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611555/
转载文章受原作者版权保护。转载请注明原作者出处!