常用类--String
练习一
package String;
import java.io.UnsupportedEncodingException;
public class Demo01 {
public static void main(String[] args) throws UnsupportedEncodingException {
//字符串连续字符组成形成的数据整体
//java.lang.String
String name = "zhengsan";
String name1 = new String("zhangsan");
char[] cs = {'a','中','国'};
byte[] bs = {-28,-72,-83,-27,-101,-67};
String s = new String(cs);
String s1 = new String(bs,"UTF-8");
System.out.println(s1);
String a = "\"";
//\,\t,\n,\\
System.out.println(a);
}
}
练习二
package String;
import java.io.UnsupportedEncodingException;
public class Demo02 {
public static void main(String[] args) throws UnsupportedEncodingException {
//拼接
String s = "a" + "b";
String s1 = "ab";
//hashCode 对象内存地址
System.out.println(s.hashCode());
System.out.println(s1.hashCode());
String s3 = 1 + "abc" + 2;
System.out.println(s3);
//concat也表示字符串连接
System.out.println(s1.concat(s3));
//字符串的比较
String a = "a";
String b = "A";
//相等
System.out.println(a.equals(b));
//忽略大小写的相等
System.out.println(a.equalsIgnoreCase(b));
//i = 正数,a>b
//i = 负数,a<b;
//i =0,a = b
int i = a.compareTo(b);
System.out.println(i);
//字符串截断操作
//substring 用于截取字符串,需要传递两个参数
//第一个参数表示截取字符串的起始位置(索引,包含)
//第二个参数表示截取字符串的结果位置(索引,不包括)
//substring方法如果只传递一个参数,那么就表示从指定位置开始截取字符串,然后截取到最后
String z = "Hello World";
System.out.println(z.substring(0,3));//Hel
System.out.println(z.substring(0,"Hello".length()));//Hello
System.out.println(z.substring(6,z.length()));//World
System.out.println(6);
System.out.println("============================");
//分解字符串,根据指定的规则对字符串进行分解,可以将一个完整的字符串,分解成几个部分
String[] z1 = z.split(" ");
System.out.println(z1.length);
System.out.println("==================");
for (String z2 : z1) {
System.out.println(z2);
}
System.out.println("==================");
// trim 去掉的是字符串首尾的空格
System.out.println(z.trim());
System.out.println("==================");
//字符串的替换
String l1 = "Java hive hadoop";
System.out.println(l1.replace("Java","python"));
//replaceAll按照指定的规则进行替换
System.out.println(l1.replaceAll("hive|hadoop","python"));
System.out.println("==================");
//字符串大小写转换
String l2= "Java Python Hadoop";
System.out.println(l2.toLowerCase());
System.out.println(l2.toUpperCase());
System.out.println("==================");
String name = "user";
String u1 = name.substring(0,1); //u
String u2 = name.substring(1);
System.out.println(u1.toUpperCase() + u2);
System.out.println("==================");
//字符串查找
//l2.toCharArray.var回车
char[] chars = l2.toCharArray();
byte[] bytes = l2.getBytes("UTF-8");
//charAt可以传递索引定位字符串中指定位置的字符
System.out.println(l2.charAt(1));
System.out.println("==================");
//indexof方法用于获取数据在字符串中第一次出现的位置
System.out.println(l2.indexOf("Hadoop"));
System.out.println("==================");
//是否包含指定的字符串,返回布尔类型
System.out.println(l2.contains("Java"));
System.out.println("==================");
//判断字符串是否以指定的数据开头,返回布尔类型
System.out.println(l2.startsWith("Java"));
//判断字符串是否以指定的数据结尾,返回布尔类型
System.out.println(l2.endsWith("Java"));
System.out.println("==================");
//判断字符串是否为空,空格其实是特殊的字符串
System.out.println(l2.isEmpty());
}
}
练习三
package String;
public class Demo03 {
public static void main(String[] args) {
//StringBuilder : 构建字符串
StringBuilder s = new StringBuilder();
for (int i = 0; i <100 ; i++) {
s = s.append(i);
}
System.out.println(s.toString());
StringBuilder s1 = new StringBuilder();
s1.append("abc");
System.out.println(s1.toString());
System.out.println(s1.length());
System.out.println(s1.reverse()); //反转字符串
System.out.println("=============");
System.out.println(s1.insert(1, "d"));
}
}