你知道多少常用类?

String
	String 不可变长的字符序列  "abc"  Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现
	其内部是由字符串组表示   private final char value[];
	String : 不可变长字符串
	StringBuilder:可变长字符串,线程不安全的
		StringBuilder() 构造一个其中不带字符的字符串生成器,初始容量为 16 个字符。
		StringBuilder sb=new StringBuilder();
		System.out.println(sb);
		System.out.println(sb.capacity());  //16
		System.out.println(sb.length());
	StringBuffer:可变长字符串,线程安全的
	string构造方法
		String() 初始化一个新创建的 String 对象,使其表示一个空字符序列
		String s1=new String();
		String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列
		String s2=new String(new char[]{'s','h','s','x','t'});
		System.out.println(s2);
		String(char[] value, int offset, int count) 分配一个新的 String,它包含取自字符数组参数一个子数组的字符
		String s3=new String(new char[]{'s','h','s','x','t'},2,3);
		System.out.println(s3);
		//String(byte[] bytes)     通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
		String string="尚学堂";
		byte[] arr=string.getBytes();
		System.out.println(Arrays.toString(arr));
	String的方法
		char charAt(int index)  返回指定索引处的 char 值。    ***
		System.out.println("charAt():"+str.charAt(2)); 
		int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引    ***
		System.out.println("indexOf():"+str.indexOf("s"));
		int indexOf(String str, int fromIndex) 
		System.out.println("indexOf(),指定起始索引:"+str.indexOf("s",1));
		int lastIndexOf(String str)   返回指定子字符串在此字符串中最右边出现处的索引。
		System.out.println("lastIndexOf():"+str.lastIndexOf("s"));
		 int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点)。 
		System.out.println("charAt():"+str.codePointAt(2));
		int compareTo(String anotherString)  按字典顺序比较两个字符串。 
		相同返回0,不同 使用当前字符串与参数字符串同一个位置的字符进行减法
		System.out.println("compareTo()"+str2.compareTo(str)); 
		int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写
		System.out.println("compareToIgnoreCase()"+str2.compareToIgnoreCase(str));
		String concat(String str) 将指定字符串连接到此字符串的结尾。   返回新串
		System.out.println("concat()"+str.concat(str2));  //shsxtgoodShsxtgood
		boolean contains(CharSequence s)  当且仅当此字符串包含指定的 char 值序列时,返回 true。
		System.out.println("contains()"+str.contains("sxt"));  //true
		static String copyValueOf(char[] data)    字符数组转为字符串
		static String copyValueOf(char[] data, int offset, int count) 
		boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。 
		boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。 
		System.out.println("endsWith()"+str.endsWith("d"));  //true
		byte[] getBytes()
		使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
		byte[] getBytes(Charset charset) 
		byte[] arr="你好".getBytes();  //utf-8
		byte[] arr2="你好".getBytes("gbk");  //gbk
		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.toString(arr2));
		// String replace(char oldChar, char newChar)  新串替换老串
		System.out.println("replace()"+str.replace("s","S"));
		//tring[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。  
		String str3="name=zhangsan";
		String[] s=str3.split("=");
		System.out.println(Arrays.toString(s));
		String substring(int beginIndex)  
		       返回一个新的字符串,它是此字符串的一个子字符串。 
		String substring(int beginIndex, int endIndex)   endIndex不包含
		       返回一个新字符串,它是此字符串的一个子字符串。 
		System.out.println("substring()"+str.substring(3)); 
		System.out.println("substring()"+str.substring(2,5));
		 char[] toCharArray() 将此字符串转换为一个新的字符数组。
		char[] ch=str.toCharArray();
		System.out.println(Arrays.toString(ch));
Math
	static double floor(double a)   向下取整
	System.out.println(Math.floor(1.98));
	static double ceil(double a)    向上取整
	System.out.println(Math.ceil(1.18));
	static double max(double a, double b)  返回两个 double 值中较大的一个。
	System.out.println(Math.max(1.1, 1));
	static int min(int a, int b)  返回两个 int 值中较小的一个。 
	System.out.println(Math.min(1.1, 2.2));//1.1
	static long round(double a)  返回最接近参数的 long。 
	System.out.println(Math.round(1.49));  //1
拆装箱
	自动装箱: 从基本数据类型->包装类型
	自动拆箱: 从包装类型->基本数据类型
	Integer 类在对象中包装了一个基本类型 int 的值
	int缓冲区对象的表示范围: [-128,127]

猜你喜欢

转载自blog.csdn.net/weixin_45116982/article/details/91410859
今日推荐