package com.Tree.javase.String;
// 关于String类中常用的方法
1 char charAt(int index) 返回指定索引处的char值
2 int compareTo(String anotherString) 按字典顺序比较两个字符串
3 boolean contains(CharSequence s) 判断当前的字符串中是否包含后面的子字符串
4 boolean endsWith(String suffix) 判断当前字符串是否以某个字符串结尾
5 boolean equals(Object anObject)
6 boolean equalsIgnoreCase(String anotherString) 在忽略大小写的情况下,判断两个字符串是否相等
7 byte[] getBytes() 将字符串对象转换成字节数组
8 int indexOf(String str) 判断某个子字符串在当前字符串中第一次出现处的索引(下标)
9 boolean isEmpty() 判断某个字符串是否为空,空格不是空字符串
10 int length() 判断字符串长度,判断数组长度是length属性,判断字符串长度是length()方法
11 int lastIndexOf(String str) 判断某个字符串在当前字符串中最后一次出现的索引(下标)
12 String replace(CharSequence target,CharSequence replacement) 字符串替换
13 String[] split(String regex) 拆分字符串
14 boolean startsWith(String prefix) 判断当前字符串是否以某个字符串开始
15 String substring(int beginIndex) 从当前字符串的某个位置开始截取字符串
16 String substring(int beginIndex,int endIndex) 左闭右开截取字符串
17 char[] toCharArray() 将一个字符串转换成char数组
18 String toLowerCase() 将所给字符串全部转换为小写
19 String toUpperCase() 将所给字符串全部转换为大写
20 String trim() 去除字符串前后空白,中间的空白不能去掉
21 valueOf() String中唯一的静态方法,通过类名String.调用,作用是将"非字符串"转换成"字符串"
public class StringTest05 {
public static void main(String[] args) {
System.out.println("1-----------");
char c="浙江省".charAt(1);
System.out.println(c); // 江
System.out.println("2-----------");
int result1="abc".compareTo("abc");
System.out.println(result1); // 0
int result2="abc".compareTo("xyz");
System.out.println(result2); // -23 // 前小后大,按照字典顺序
System.out.println("3-----------");
System.out.println("HelloWorld.java".contains("java")); // true // 判断当前的字符串中是否包含后面的子字符串
System.out.println("http://www.baidu.com".contains("wwww.baidu")); // false
System.out.println("4-----------");
System.out.println("Test.java".endsWith("java")); // true
System.out.println("Test.java".endsWith("abcd")); // false
System.out.println("5-----------");
System.out.println("abc".equals("abc")); // true
System.out.println("6-----------");
System.out.println("AbC".equalsIgnoreCase("ABC")); // true
System.out.println("7-----------");
byte[]bytes="abcdefgh".getBytes();
for (int i=0;i<bytes.length;i++){
System.out.print(bytes[i]+" "); // 97 98 99 100 101 102 103 104
}
System.out.println();
System.out.println("8-----------");
System.out.println("oraclejava.python.c++.java".indexOf("java")); // 6
System.out.println("9-----------");
String s="";
System.out.println(s.isEmpty()); // true
System.out.println("10-----------");
System.out.println("abcd".length()); // 4
System.out.println("11-----------");
System.out.println("java.python.c++.MySQL.java".lastIndexOf("java")); // 22
System.out.println("12-----------");
System.out.println("http://www.baidu.com".replace("http:","https:")); // https://www.baidu.com
System.out.println("13-----------");
String[] str="2021-8-6".split("-");
for(int i=0;i<str.length;i++){
System.out.print(str[i]+" "); // 2021 8 6
}
System.out.println();
System.out.println("14-----------");
System.out.println("http://www.baidu.com".startsWith("http")); // true
System.out.println("15-----------");
System.out.println("http://www.baidu.com".substring(7)); // www.baidu.com
System.out.println("16-----------");
System.out.println("http://www.baidu.com".substring(7,10)); // www
System.out.println("17-----------");
char[] a="我是一个java工程师".toCharArray();
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" "); // 我 是 一 个 j a v a 工 程 师
}
System.out.println();
System.out.println("18-----------");
System.out.println("ABDEGASsfsfs".toLowerCase()); // abdegassfsfs
System.out.println("19-----------");
System.out.println("svhfgavdfhfb".toUpperCase()); // SVHFGAVDFHFB
System.out.println("20-----------");
System.out.println(" hello world! ".trim()); // hello world!
System.out.println("21-----------");
String s1=String.valueOf(100);
System.out.println(s1); // 100,字符串
}
}
String类型常用方法详解
最新推荐文章于 2022-12-10 11:22:16 发布