public class Compound {
public static void main(String args[]) {
String a = "good";
System.out.println(a);
char b[] = {'h','e','l','l','o'};
String s = new String(b);
System.out.println(a);
char d[] = {'h','e','l','l','o'};
String c = new String(d,2,3);
System.out.println(c);
}
}
public class Compound {
public static void main(String args[]) {
String a = "good";
System.out.println(a);
char b[] = {'h','e','l','l','o'};
String s = new String(b);
System.out.println(a);
char d[] = {'h','e','l','l','o'};
String c = new String(d,2,3);
System.out.println(c);
System.out.println(s+' '+a);
int myint = 1;
System.out.println(s + ' ' + a + myint);
String str = s + ' ' + a + myint;
int size = str.length();
System.out.println(size);
}
}
public class LearnString {
/**
* 字符串的普通使用
* @param args
*/
public static void main(String[] args) {
String str = "we are student";
//字符串长度
System.out.println(str.length());
//字符串查找
System.out.println(str.indexOf("a"));//第一次出现的位置
System.out.println(str.lastIndexOf("e"));//最后一次出现的位置
//获取指定位置的字符
System.out.println(str.charAt(3));
//截取字符串
System.out.println(str.substring(3,3));
//去除空格
System.out.println(str.trim());
//字符串替换
System.out.println(str.replace("are","is"));
//判断字符串的开始和结尾
System.out.println(str.startsWith("we"));
System.out.println(str.startsWith("wee"));
System.out.println(str.endsWith("we"));
System.out.println(str.endsWith("nt"));
String str_a = "abC def";
String str_b = "abc def";
boolean b_bool = (str_a == str_b);
System.out.println(b_bool);
System.out.println(str_a.equals(str_b));
System.out.println(str_a.equalsIgnoreCase(str_b));
//字母大小写转换
System.out.println(str_a.toLowerCase());
System.out.println(str_b.toUpperCase());
//字符串分割
String str_arr[] = str_a.split(" ");
for (String item : str_arr) {
System.out.println(item);
}
}
}
import java.util.Date;
public class LearnString2 {
/**
* 格式化字符串
* @param args
*/
public static void main(String[] args) {
Date date = new Date();
String date_s = String.format("%tF",date);
System.out.println(date_s);
}
}
public class LearnString3 {
/**
* 使用正则表达式
* @param args
*/
public static void main(String[] args) {
String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
String str1 = "www.aa@";
String str2 = "www";
String str3 = "[email protected]";
if (str1.matches(regex)) {
System.out.println(1+" "+true);
}
if (str2.matches(regex)) {
System.out.println(2+" "+true);
}
if (str3.matches(regex)) {
System.out.println(3+" "+true);
}
}
}
public class LearnString4 {标签:java,String,生成器,System,println,str,字符串,public,out From: https://blog.51cto.com/u_15826214/5750134
/**
* 字符串生成器
*/
public static void main(String[] args) {
String str = "";
long start_time = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
str = str + i;
}
long end_time = System.currentTimeMillis();
long time = end_time-start_time;
System.out.println("String"+ time);
System.out.println("String:" + str);
StringBuilder builder = new StringBuilder();
start_time = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
builder.append(i);
}
end_time = System.currentTimeMillis();
time = end_time-start_time;
System.out.println("Builder"+ time);
System.out.println("Builder:"+ builder.toString());
}
}