由于初学所以会有很多不完善的地方,所以此博客不定时更新。
public class Main{
public static boolean check(String str)
{
for(int i=0; i < str.length(); i++)
{
if(str.charAt(i) >= '0' && str.charAt(i) <= '9')
continue;
else
return false;
}
return true;
}
public static boolean check(char str[])
{
for(char c : str)
{
if(c < '0' || c > '9')
return false;
}
return true;
}
public static void main(String args[]) {
// 静态常量池体现
String str1 = "www.baidu.com";
String str2 = "www." + "baidu" + ".com"; // 编译时就能确定str2的值
System.out.println(str1 == str2);
// 动态常量池体现
String tmp = "baidu";
String t1 = "www.baidu.com";
String t2 = "www." + tmp + ".com"; // 运行时才能确定t2的值
String t3 = "www." + tmp + ".com";
System.out.println(t1 == t2);
System.out.println(t2 == t3);
System.out.println(str1.length());
System.out.println(str1.charAt(3));
char[] res = str1.toCharArray();
for(int i = 0; i < res.length; i++)
{
if(res[i] != '.')
res[i] -= 'a' - 'A';
System.out.print(res[i]);
}
System.out.println();
String newStr = new String(res, 0, 5);
System.out.println(newStr);
String s = "12345";
System.out.println(check(s.toCharArray()));
byte data[] = str1.getBytes();
for(int i=0; i < data.length; i++)
{
data[i] -= 'a' - 'A';
}
newStr = new String(data);
System.out.println(newStr);
}
}
class StringUtil{
// 首字母大写
public static String upprtFirst(String str)
{
// if(str == null || "".equals(str))
if(str == null || str.isEmpty())
return str;
if(str.length() == 1)
return str.toUpperCase();
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}
public class Main{
public static void main(String args[]){
String str1 = "abc";
String str2 = "ABC";
System.out.println(str1.contentEquals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
// 返回int型数据,比较的是两个字符串的大小
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareToIgnoreCase(str2));
// 字符串查找
System.out.println("-------------");
String str = "123456789101234";
System.out.println(str.indexOf("456")); // 从头开始查找
System.out.println(str.indexOf("456", 4)); // 从指定位置开始查找
System.out.println(str.contains("456"));
System.out.println(str.lastIndexOf("456")); // 从后向前开始查找
System.out.println(str.lastIndexOf("456", 2));
System.out.println(str.startsWith("123")); // 判断是否以指定字符串开头
System.out.println(str.startsWith("123", 2));
// endsWidth同上
// 字符串替换
System.out.println(str.replaceAll("1", "x"));
System.out.println(str.replaceFirst("1", "x"));
System.out.println(str.replace("1234", "abcd"));
System.out.println(str.replace("23", "ff"));
// 字符串拆分
String s = "www.baidu.com";
String ss[] = s.split("\\.", 2); // 拆分为2个,第二个参数可选
for(int i=0; i < ss.length; i++)
System.out.println(ss[i]);
// 字符串截取
str = "0123456789";
System.out.println(str.substring(1));
System.out.println(str.substring(1, 7));
System.out.println(str.subSequence(1, 7));
// 字符串格式化
String name = "张三";
int age = 18;
double score = 98.7654321;
String t = String.format("姓名:%s, 年龄: %d, 分数:%5.2f", name, age, score);
System.out.println(t);
// 其他方法
String s1 = "www.baidu.com";
String s2 = "www.".concat("baidu").concat(".com"); // 动态过程,和+有区别
System.out.println(s2);
System.out.println(s1 == s2);
System.out.println("".isEmpty()); // 判断是否为空字符串,不是null
System.out.println(" abc ".trim()); // 去除左右空格
System.out.println(s1.toUpperCase()); // 转大写
System.out.println("ABC".toLowerCase()); // 转小写
}
}