String引用类型,它提供如下方法:
String str = 123;
String str = “aaa”;
方法名 |
说明 |
length() |
字符串长度 |
charAt() |
提取字符串中的字符 |
equals() |
比较字符串 |
equalsIgnoreCase() |
比较时忽略大小写 |
toLowerCase() |
转小写 |
toUpperCase() |
转大写 |
concat() |
连接字符串 |
indexOf() |
查找字符串b在a中首次出现的位置,如果查找不到, 返回-1 |
lastIndexOf() |
查找字符串b在a中最后一次出现的位置 |
substring() |
提取子字符串 |
trim() |
去掉字符串首尾的空格 |
split() |
分割字符串,返回String[] |
contains() |
判断字符串a中是否包含字符串b |
valueOf() |
将基本类型转换为字符串型 |
replace() |
将字符串中的旧字符替换为新的字符 |
startsWith() |
以xxx开头 |
endsWith() |
以xxx结尾 |
toCharArray() |
转换成字符数组 |
getBytes() |
将字符串转换为字节数组 |
String(byte[] data, String[] charset) |
将字节数组转换为字符串 |
字符串的不变性:
对字符串a调用以上的方法时, 返回的都是新的字符串, 不会对原字符串对象a做出改变, 除非给a重新赋值.
StringBuffer:String增强版
对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率
方法名 |
说明 |
append() |
末尾追加内容 |
reverse() |
反转 |
insert() |
插入字符 |
delete() |
删除字符 |
toString() |
转换为String形式 |
StringBuffer是可变的, 只要调用了 append等方法, 它的值就改变了.
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import org.junit.Test;
public class StringTest {
@Test
public void test() {
String str = new String("hello");
System.out.println(str);
String str2 = "hello";
String str = "hello";
int n= str.length();
System.out.println(str == str2);
System.out.println(n);
}
@Test
public void test1() {
String str = "hello";
String str2 = "heLLo";
if(str.equalsIgnoreCase(str2)){
System.out.println("相等的");
}else{
System.out.println("两个字符串不相等");
}
}
@Test
public void test2() {
String str = "HELL23432o";
String str2 = "heLLo";
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
}
@Test
public void test3() {
String str = "123";
int a = 123;
String st = str+a;
System.out.println("*---------------------------------*");
String st2 = str.concat("123");
System.out.println(st);
System.out.println(st2);
System.out.println(str+123);
}
@Test
public void test4() {
String str = "hello,JQK is JAVA not JAVA";
String str2 = "JAVA";
System.out.println(str.indexOf(str2,14));
System.out.println(str.lastIndexOf(str2));
}
@Test
public void test5() {
String str = "hello,Jhello,QK is JAVA not JAVA";
System.out.println(str.substring(5, 9)); [ )
Math.random(); [0,1)
}
@Test
public void test6() {
String str = "hello,Jhello,QK is JAVA not JAVA";
System.out.println(str.substring(5, 9)); [ )
Math.random(); [0,1)
}
@Test
public void test7() {
String str = " he llo ";
System.out.println(str.trim());
}
@Test
public void test8() {
String str = "春眠不觉晓 处处闻啼鸟 夜来风雨声 花落是多少";
/*
春眠不觉晓
处处闻啼鸟
夜来风雨声
花落是多少
*/
String [] arr = str.split(" ");
String [] arr = {"春眠不觉晓","处处闻啼鸟","夜来风雨声","花落是多少"};
for (String st : arr) {
System.out.println(st+"。");
}
}
@Test
public void test9() {
String str = "20180706";
String str2 = "809";
if(str.contains(str2)){
System.out.println("模糊查询到了");
}else{
System.out.println("没找到");
}
}
@Test
public void test10() {
int a = 0123;
System.out.println(String.valueOf(a));
}
@Test
public void test11() {
String str = "小笨蛋";
String str2 = "笨蛋";
System.out.println(str.replace("笨蛋", "**"));
}
@Test
public void test12() {
String str = "张一凡AAABBBB";
if(str.startsWith("王") && str.contains("AA") && str.endsWith("B")){
}
System.out.println(str.startsWith("王"));
System.out.println(str.endsWith("B"));
}
@Test
public void test13() {
String str = "hello";
char [] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.println(ch[i]);
}
for (char c : ch) {
System.out.println(c);
}
}
@Test
public void test14() throws UnsupportedEncodingException {
String str = "哈喽你好a";
byte [] by = str.getBytes("UTF-8");
System.out.println(Arrays.toString(by));
}
@Test
public void test15() throws UnsupportedEncodingException {
byte [] by = {-27, -109, -120, -27, -106, -67, -28, -67, -96, -27, -91, -67, 97};
String st = new String(by,"UTF-8");
System.out.println(st);
}
@Test
public void test16(){
long start = System.currentTimeMillis(); //获取当前时间
StringBuffer str = new StringBuffer("ch");
String str = "ch";
for(int i = 1; i <= 10; i++){
str.append("aa");
str += "aa";
}
long end = System.currentTimeMillis(); //获取当前时间
System.out.println(str);
String str2 = str.toString();
System.out.println(str2);
System.out.println("执行了"+(end-start)+"毫秒");
}
@Test
public void test17(){
String str = new String("hello");
StringBuffer strb = new StringBuffer("hello");
strb.insert(2, "world");
strb.delete(2, 7);
System.out.println(strb);
System.out.println(strb.reverse());
}
}