string类:
/*
java.lang.String 代表字符串
java中凡是 ""括起来的 都是String类的实例(对象)
字符串值不可改变的,一旦字符串对象被创建,值就不能改变了
底层存储字符串内容的数组,是被final修饰的,必须在对象创建之初由构造方法对其赋值
private final char value[];
String s = "abc"; abc
s += "aaa"; abcaaa
s += "bbb"; abcaaabbb
s += "ccc"; abcaaabbbccc 每次+= + 其实都是创建了一个新的字符串对象
java中字符串创建的两种方式
方式1:
String s1 = "abc";
String s2 = "abc";
在第一次创建s1变量时,会去内存中有一个叫字符串常量池的空间检索,有没有此内容的一个字符串对象,
如果没有,就会在字符串常量池中创建一个字符串对象,把对象的地址赋给s1.
在第二次创建s2变量时,会去字符串常量池中查找,如果有,直接将之前创建的字符串对象赋值给s2.
一旦出现要创建的字符串对象内容一致,返回拿到的是同一个字符串对象的地址
方式2:
String s3 = new String("abc");
无论是否存在相同内容的字符串对象,都会创建一个新的字符串对象
*/
String 构造方法
String();
String("abc");
String(byte[] b)
String(char[] c)
/*
判断功能
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean isEmpty()
boolean startsWith(String prefix)
boolean endsWith(String suffix)
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcd"; //null-关键字 ""-->字符串对象的值是""
System.out.println(s.equals("abcD"));
System.out.println(s.equalsIgnoreCase("abcD"));//忽略大小写
System.out.println(s.contains("a"));
System.out.println(s.contains("ab"));//判断是否包含子串(子串必须是连续的)
System.out.println(s.contains("ac"));//false
System.out.println(s.isEmpty());//判断是否是空串""
System.out.println(s.startsWith("ab"));//判断是否以指定的子串开头
System.out.println(s.endsWith("bd"));//判断是否以以指定的
/*
获取功能
int length()
char charAt(int index)
int indexOf(String str)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcdcde";
//0123456
System.out.println(s.length());
System.out.println(s.charAt(2));
System.out.println(s.indexOf("c"));//返回指定字符首次出现的位置
System.out.println(s.indexOf("cd"));//返回指定字符串首字母首次出现的位置
System.out.println(s.indexOf("c", s.indexOf("c")+1));//从指定的位置开始查找
System.out.println(s.lastIndexOf("c"));//从后向前查找
//String s1 = s.substring(3);//从指定的位置开始截取字符串,返回一个新的子字符串
String s1 = s.substring(0,4);//从指定的位置开始到指定的位置结束(不包含结束)截取字符串,返回一个新的子字符串
/*
转换功能
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
String toLowerCase()
String toUpperCase()
String concat(String str)
Stirng[] split(分割符);
*/
public static void main(String[] args) {
Integer a = null;
//System.out.println(a.toString());
String s = String.valueOf(a);//把其他类型转为字符串 建议使用的,避免出现空指针异常
System.out.println(s);
char[] c = {'a','b','c'};
String s1 = String.valueOf(c);
String s2 = "abcDEF你好";
System.out.println(s2.toLowerCase());
System.out.println(s2.toUpperCase());
String s3 = s2.concat("wwwww");//将指定字符串拼接到字符串末尾,返回一个新的字符串
System.out.println(s3);
String s4 = "ab;cde;fg";
String [] strings = s4.split(";"); // 使用指定的字符将字符串分割为数组 正则表达式
/*
替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
● 去除字符串两空格
String trim()
*/
public static void main(String[] args) {
String s = " abcd cefg ";
System.out.println(s.replace('c', 'C'));
System.out.println(s.replace("cd", "CC"));
// replaceAll() 使用正则表达匹配需要替换的内容
System.out.println(s.replaceAll("c", "CC"));
System.out.println(s.replaceFirst("c", "CC"));
System.out.println(s.length());
System.out.println(s.trim().length());
System.out.println(s.replace(" ", "").length());
Randem类:
Random random = new Random();
System.out.println(random.nextInt());//在int取值范围内随机返回一个结果
System.out.println(random.nextInt(30));//在指定的范围内返回一个随机数 大于等于0 小于给定的值
Math类:
System.out.println(Math.random());//返回一个大于等于0 小于1的一个随机数