二、String 类
1. 常用方法
三、时间日期类
package com.binge.myblogsystem;
public class Test {
public void testBase() {
// 定义字符串
String str = " hahaha ";
// 获取字符串长度
System.out.println(str.length());
// 忽略字符串两边的空行
System.out.println(str.trim());
// 获取指定索引出的字符
System.out.println(str.charAt(3));
// 将字符串转换并存储在字符数组中
char[] chardst = new char[1024];
str.getChars(0, 7, chardst, 0);
System.out.println(chardst);
}
public void testCompare() {
String str1 = "elapant";
String str2 = "ELEPANT";
String str3 = "Apple";
String str4 = "apple";
// compareTo()方法:不忽略大小写
if (str1.compareTo(str2) > 0) {
System.out.println(str1 + ">" + str2);
} else if (str1.compareTo(str2) == 0) {
System.out.println(str1 + "=" + str2);
} else {
System.out.println(str1 + "<" + str2);
}
// compareToIgnoreCase()方法:忽略大小写
if (str1.compareToIgnoreCase(str2) > 0) {
System.out.println(str1 + ">" + str2);
} else if (str1.compareToIgnoreCase(str2) == 0) {
System.out.println(str1 + "=" + str2);
} else {
System.out.println(str1 + "<" + str2);
}
// equals()方法:不忽略大小写
if (str3.equals(str4)) {
System.out.println(str3 + "=" + str4);
} else {
System.out.println(str3 + "!=" + str4);
}
// equalsIgnoreCase()方法:忽略大小写
if (str3.equalsIgnoreCase(str4)) {
System.out.println(str3 + "=" + str4);
} else {
System.out.println(str3 + "!=" + str4);
}
}
public void testConvert() {
/**
* 将字符串转为其他数据类型
*/
boolean bool = Boolean.getBoolean("false");
Integer integer = Integer.getInteger("20");
byte parseByte = Byte.parseByte("20");
float parseFloat = Float.parseFloat("20.2");
double parseDouble = Double.parseDouble("20.2");
Long long1 = Long.getLong("20");
/**
* 将其他数据类型转为字符串:valueOf
*/
String all = String.valueOf(bool);
}
public void testFind() {
// 定义字符串
String str = "How qi bocome handsome like qi ge";
// indexOf(string, index):查找首个字符串出现的位置
System.out.println(str.indexOf("qi", 4));
// lastIndexOf(string, index):查找最后一个字符串出现的位置
System.out.println(str.lastIndexOf("qi", 10));
}
public void testMain() {
// 定义字符串
String str = "How to cut and split strings";
// 截取指定索引范围的字符串:substring(startIndex, endIndex)
System.out.println(str.substring(0, 10));
// 按照指定正则表达式拆分字符串:split(regex)
String[] split = str.split(",");
for (String string : split) {
System.err.println(string);
}
// 拼接字符串:concat(str)
System.out.println(str.concat("I'm OK") + "或者是" + str + "这样也可以拼接");
// 字符串替换:replace(oldStr, newStr)
System.out.println(str.replace("I'm OK", "I'm new string"));
}
}
标签:String,核心,str1,System,str,println,JavaSE,out From: https://www.cnblogs.com/StarLemon/p/17745100.html