- String类的转换
- String类中常用的方法
方法 | 含义 |
---|---|
boolean equals(String) | 判断两个字符串对象的内容是否相等 |
boolean equalsIgnoreCase(String) | 比较两个字符串的内容是否相等,忽略大小写 |
String toUpperCase( ) | 将String对象中的所有字符都转换为大写 |
String toLowerCase( ) | 将String对象中的所有字符都转换为小写 |
char charAt(int) | 返回指定索引处的 char 值 |
String substring(int begin) | 返回一个新字符串,该字符串是从begin开始的字符串的内容 |
String substring(int begin,int end) | 返回一个新字符串,该字符串是从begin开始到end-1结束的字符串的内容 |
int indexOf/lastIndexOf(char) | 返回指定字符在此字符串中第一次/最后一次出现处的索引。 |
int indexOf/lastIndexOf(char,int) | 从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符处的索引 |
int indexOf/lastIndexOf(String) | 返回第一次出现的指定子字符串在此字符串中的索引 |
int indexOf/lastIndexOf(String,int) | 从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符串处的索引 |
String trim( ) | 返回新的字符串,忽略前导空白和尾部空白 |
int length( ) | 返回此字符串的长度 |
String concat(String str) | 将指定字符串连接到此字符串的结尾 |
byte[] getBytes() | 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中 |
byte[] getBytes(Charset charset) | 使用给定的 charset将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组 |
String[] split(String regex) | 根据给定正则表达式的匹配拆分此字符串。 |
String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的 |
boolean startsWith(String prefix) | 测试此字符串是否以指定的前缀开始 |
boolean endsWith(String suffix) | 测试此字符串是否以指定的后缀结束 |
示例代码:
package 字符串类;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
public class TestAPI {
public static void main(String[] args) {
// charAt
String s = new String("agddsfsdgdgdsgsdgsdj");
System.out.println(s.charAt(2));//d 索引从0开始
//concat()
System.out.println(s.concat("123"));//产生一个新的字符串:s不变agddsfsdgdgdsgsdgsdj123
System.out.println(s);//agddsfsdgdgdsgsdgsdj
//contains()
System.out.println(s.contains("123"));//false
//需求:判断一个文件是否是图片文件,图片文件的后缀:jpg JPG png PNG gif GIF
String fileName = "hello.gifx";
if(!fileName.endsWith("gif") || !fileName.endsWith("GIF")) {
System.out.println(fileName + "不是图片格式的文件");
}
//equalsIgnoreCase()忽略大小写比较,常用于验证码等。。。。
System.out.println("abc".equalsIgnoreCase("ABC"));//true
//getBytes() String ------> byte[]
//byte[]: [97, 103, 100, 100, 115, 102, 115, 100, 103, 100, 103, 100, 115, 103, 115, 100, 103, 115, 100, 106]
System.out.println("byte[]: " + Arrays.toString(s.getBytes()));
//使用指定的字符集给String进行编码得到对应的字节数组
// byte[]: [97, 103, 100, 100, 115, 102, 115, 100, 103, 100, 103, 100, 115, 103, 115, 100, 103, 115, 100, 106]
System.out.println("byte[]: " + Arrays.toString(s.getBytes(Charset.defaultCharset())));
//byte[] ----> String
byte[] b = s.getBytes();
String k = null;
try {
k = new String(b,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("k: " + k);
System.out.println("k.inexOf('a'): "+ k.indexOf('a'));
//需求:找出字符串k中d的出现次数
int count = 0;//存放找的次数
int index = 0;//从哪里找
while((index = k.indexOf('d',index + 1)) != -1) {
System.out.println("index:" + index);
index++;
count++;//找到了,count + 1
}
System.out.println("count: " + count);
System.out.println(k.length());
String email = "abc@163.com.com";
int firstIndex = email.indexOf('.');//从头开始数.最开始出现的下标
int lastIndex = email.lastIndexOf('.');//从头开始数.最后出现的下标
System.out.println("firstIndex: " + firstIndex);
System.out.println("lastIndex: " + lastIndex);
if(firstIndex != lastIndex) {
System.out.println("邮箱格式不合法");
}
//replaceAll()
String str = "我不爱你们,我恨你们,我恨死你们了!";
str.replaceAll("恨","喜欢");
System.out.println(str.replaceAll("恨","喜欢"));
//split()
String source = "a6b6afj6wou6kjs";
String[] result = source.split("6");
System.out.println(Arrays.toString(result));
//substring()
String a="asjfda;dsfaj;";
System.out.println(a.substring(3));//fda;dsfaj;
System.out.println(" a b ".trim());//只能去掉首尾的空格
}
}
知识拓展:
以上就是我关于 Java-String类中常用的方法 知识点的整理与总结的全部内容,另附源码
分割线
博主为咯学编程:父母不同意学编程,现已断绝关系;恋人不同意学编程,现已分手;亲戚不同意学编程,现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏,金额不限。。。