Java的String类
-
字符串是常量,创建之后不可改变
-
字符串字面值存储在字符池中,可以共享。
-
String s = "Hello";产生一个对象,字符串池中存储
-
String s = new String("Hello");//产生两个对象,堆和池各存储一个。
-
package com.cnblogs;
public class Application {
public static void main(String[] args) {
String name = "Hello";
name = "张三";
String name2 = "张三";
String str = new String("Java是最好的编程语言");
String str2 = new String();
System.out.println(str == str2);
System.out.println(str.equals(str2));
}
}
常用方法
-
public int length():返回字符串的长度
-
public char charAt(int index);根据下标获取字符
-
public boolean contains(String str);判断当前字符串中是否包含str
package com.cnblogs;
public class Application {
public static void main(String[] args) {
String str ="Java是最好的编程语言";
System.out.println(str.length());
System.out.println(str.charAt(str.length() - 1));
System.out.println(str.contains("Java"));
System.out.println(str.contains("php"));
}
}
-
public char[] toCharArray();将字符串转成数组。
-
public int indexOf(String str);查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1;
-
public int lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标索引。
package com.cnblogs;
import java.util.Arrays;
public class Application {
public static void main(String[] args) {
String str ="Java是最好的编程语言,Java,Java很棒";
System.out.println(Arrays.toString(str.toCharArray()));
System.out.println(str.indexOf("Java"));
System.out.println(str.indexOf("程"));
System.out.println(str.indexOf("php"));
System.out.println(str.indexOf("Java" , 4));
System.out.println(str.lastIndexOf("Java"));
}
}
-
public String trim();去掉字符串前后的空格
-
public String toUpperCase();将小写转成大写
-
public String toLowerCase();将大写转成小写
-
public boolean endWith(String str);判断字符串是否以str结尾
-
public booleanstartsWith(String str);判断字符串是否以str开头
package com.cnblogs;
import java.util.Arrays;
import java.util.Locale;
public class Application {
public static void main(String[] args) {
String str =" Hello World ";
System.out.println(str.trim());
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
String str2 = "Application.java";
System.out.println(str2.endsWith(".java"));
System.out.println(str2.startsWith("Application"));
}
}
-
public String replace(char oldChar,char newChar);将旧字符串替换成新字符串
-
public String[] split(String str);根据str做拆分
package com.cnblogs;
import java.util.Arrays;
import java.util.Locale;
public class Application {
public static void main(String[] args) {
String str = "Java是最好的编程语言";
System.out.println(str.replace("Java","php"));
String str2 = "java is the best programing language";
String str3 = "java is the best programing language,hello,World";
String str4 = "java is the best programing language,hello,,,World";
String[] arr = str2.split(" ");
String[] arr2 = str3.split("[ ,]");
String[] arr3 = str4.split("[ ,]+");
System.out.println(arr.length);
System.out.println(arr2.length);
System.out.println(arr3.length);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
}
}
package com.cnblogs;
import java.util.Arrays;
import java.util.Locale;
public class Application {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "Hello";
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
String str3 = "abc";
String str4 = "xyz";
System.out.println(str3.compareTo(str4));
String str5 = "abc";
String str6 = "abcxyz";
System.out.println(str5.compareTo(str6));
}
}
常用方法的练习
package com.cnblogs;
import java.util.Arrays;
public class TestString {
public static void main(String[] args) {
String str = "this is a text";
String[] arr = str.split(" ");
System.out.println(Arrays.toString(arr));
String str2 = str.replace("text","practice");
System.out.println(str2);
String[] arr2 = Arrays.copyOf(arr,5);
String ch;
ch = arr2[3];
arr2[3] = "easy";
arr2[4] = ch;
System.out.println(Arrays.toString(arr2));
String str3 = str.replace("text","easy test");
for (int i = 0; i < arr.length; i++) {
char first = arr[i].charAt(0);
char upperfirst = Character.toUpperCase(first);
String news = upperfirst + arr[i].substring(1);
System.out.println(news + " ");
}
}
}
可变字符串
-
StringBuffer:可变长字符串,JDK1.0提供,运行较慢,线程安全
-
StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全
package com.cnblogs;
public class TestString {
public static void main(String[] args) {
StringBuilder str = new StringBuilder();
str.append("java世界第一");
System.out.println(str.toString());
str.append(",java真香");
System.out.println(str.toString());
str.append(",java真不错");
System.out.println(str.toString());
str.insert(0,"我加在了最前面!");
System.out.println(str.toString());
str.replace(0,4,"我住在了");
System.out.println(str.toString());
str.delete(0,4);
System.out.println(str.toString());
str.delete(0,str.length());
System.out.println(str.toString());
}
}
package com.cnblogs;
public class TestString {
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 99999; i++) {
sb.append(i);
}
System.out.println(sb);
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - start));
}
}
出处:https://www.cnblogs.com/fangweicheng666/p/14993481.html