String类:
获取字符串的内容
一、获取字符串长度
public int length() {
return value.length;
}
length 与 lenght()的区别:
前面是属性,不需要传参数
后面是方法
二、指定下角标,返回固定位置的对应字符串
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
三、返回一个字符在字符串中第一次出现的位置
public int indexOf(String str) {
return indexOf(str, 0);
}
四、返回一个字符在指定位置后,出现的位置
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
五、返回一个字符(字符串)在另一个字符串中最后一次出现的位置
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
六、获取字符串的一部分
String substring(int beginIndex,int endIndex);
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
七、转换
1.将字符串转换为字节数组
public byte[] getBytes(Charset charset) {
if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
2.将字符串全部转成小写
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
Now check if there are any characters that need to be changed.
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
3.将字符串全部转为大写
String.toUpperCase(Locale.ROOT);
4.切割,将字符串切割为指定要求的字符串数组,参数为正则表达式
String[] split
5,替换String replase(char oldchar.char newchar);
String replase(String oldString.String newString);
6.去掉字符串中两端空格去掉:String trim()
7.两个字符串的字典形式比较
int compareTo(String s)
import java.nio.charset.StandardCharsets;
import java.util.Locale;
public class getStringDome {
public static void main(String[] args){
methon_1();
methon_2();
methon_3();
menth_4();
methon_5();
}
public static void methon_1(){
String a = "asdfhgvscasdf";
System.out.println(a.length());
System.out.println(a.charAt(3));
System.out.println(a.indexOf("s"));
System.out.println(a.indexOf("s",2));
System.out.println(a.lastIndexOf("s"));
System.out.println(a.lastIndexOf("s",4));
System.out.println(a.substring(2,5));
//遍历字符串
// for (int x = 0;x<a.length();x++){
// System.out.println(a.charAt(x));
// }
}
public static void methon_2(){
byte[] bytes="张三".getBytes(StandardCharsets.UTF_8);
for (int x = 0;x < bytes.length;x++){
System.out.println(bytes[x]);}
byte[] c = {-27,-68,-96};
String m = new String(c);
System.out.println(m);
}
public static void methon_3(){
String s1 = "AJDSKVvdknvv";
s1 = s1.toLowerCase(Locale.ROOT);
System.out.println(s1);
String s2;
s2 = s1.toUpperCase(Locale.ROOT);
System.out.println(s2);
}
public static void menth_4(){
String a ="asdkvjbwiejfhljzcnvndj";
String[] a1 = a.split("j");
for (int x = 0;x <a1.length;x++){
System.out.println(a1[x]);}
System.out.println("==========================");
String b = "192.168.0.1";
String[] b1 =b.split("\\.");
for (int x = 0;x <a1.length;x++){
System.out.println(b1[x]);}
System.out.println("==========================");
}
public static void methon_5(){
String c = "zxcvb" ;
String c1 = "asdf";
int c2 = c.compareTo(c1);
System.out.println(c2);
}
|