String类:
构造方法:
public String():无参构造
public String(byte[] bytes,int offset,int length):把一个字节数组的一部分转成字符串
public String(char[] value,int offset,int count)把一个字符数组的一部分转成字符串
判断功能:
boolean equals(Object obj):比较字符串的内容是否相等,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相等,不区分大小写
boolean contains(String str):判断大字符串是否包含小字符串
boolean startsWith(String str):判断字符串是否以指定字符开始
boolean endsWith(String str):判断字符串是否以指定字符结束
boolean isEmpty():判断字符串是否为空
获取功能:
int length():获取字符串的长度
char charAt(int index):获取指定位置的字符
int indexOf(int ch):获取指定字符在字符串中第一次出现的位置
int indexOf(int ch,int fromIndex):获取指定字符,从fromIndex开始,第一次出现的位置
String substring(int start,int end):获取start到end的字符串.包左不包右
转换功能:
byte[] getBytes():把字符串转成byte数组
char[] toCharArray():把字符串转成char数组
static String valueOf(char[] chs):把char数组转成字符串 注意:这个方法能把任意的数据类型都转成字符串。
String toLowerCase():把字符串转成小写字母
String toUpperCase():把字符串转成大写字母
替换功能:
String replace(String old,String new)
String trim()去除字符串两空格
int compareTo(String str)按字典顺序比较两个字符串 区分大小写
int compareToIgnoreCase(String str) 按字典顺序比较两个字符串 不区分大小写
注意:
String s = new String(“hello”);和 String s = “hello”;的区别?
前者建立了2个对象(字符串常量“hello” 常量区和s对象 堆区),后者只建立了1个对象(字符串常量“hello")。
如果是两个字符串常量相加,先在常量池里找有没有相同的结果,如果有就不新建了。
内存四区:
栈区:空间小,不需要我们自己管理,操作系统自动管理,先进后出。
堆区:空间大,需要我们自己手动管理,先进先出。在jAVA中一般是存放new出来的东西。
静态区(常量区,全局区):主要是用在存放静态变量、全局变量和常量。
系统区:操作系统使用,我们一般不能操作。
==和equals方法区别?
==:
基本类型:比较的是值是否相等
引用类型:比较的是2个对象的地址值是否相等
equals:比较两个内容是否相等
StringBuffer:
构造方法:
public StringBuffer() :无参构造
public StringBuffer(int capacity):指定容量的构造方法
public StringBuffer(String str):把String对象转成Stringbuffer
添加功能:
public StringBuffer append(String str):把任意类型的数据添加到字符串缓冲区
public StringBuffer insert(int offset,String str):把任意类型的数据插入到指定位置
删除功能:
public StringBuffer deleteCharAt(int index):删除指定位置的字符
public StringBuffer delete(int start,int end):删除指定区间的字符.包左不包右
替换功能:
public StringBuffer replace(int start,int end,String str)
反转功能:
public StringBuffer reverse()
截取功能:
public String substring(int start)
public String substring(int start,int end)
注意:
String,StringBuffer,StringBuilder的区别?
(1)String是内容不可变的,其他2个都是内容可变的。
(2)StringBuffer是同步的,数据安全,效率低
StringBuilder是不同步的,数据不安全,效率高。
StringBuffer和数组的区别?
这两个都可以看做一个容器,用来装数据。
StringBuffer是用来装字符串数据的。
数组可以放置多种数据,但必须是同一种数据类型的。
Arrays类:
public static String toString(int[] a):把一个数组转成字符串。
public static void sort(int[] a):快速排序
public static int binarySearch(int[] a,int key) 数组先要排好序在搜索
包装类:
作用:把基本数据类型包装成对象。
最常用的操作是:实现基本类型和字符串的相互转换。
Integer类:
public Integer(int value):把int转成Integer
public Integer(String s):把String转成Integer 注意:这个String必须是由数字组成的。
int类型和String类型的相互转换
String.valueOf(int number);
Integer.parseInt(s)
常用的基本进制转换
public static String toBinaryString(int i):把十进制转成二进制
public static String toOctalString(int i):把十进制转成八进制
public static String toHexString(int i):把十进制转成十六进制
十进制到其他进制
public static String toString(int i,int radix):进制的范围是 2-36 为什么?0-9,a-z
其他进制到十进制
public static int parseInt(String s,int radix)
Math类:
public static int abs(int a):绝对值
public static double ceil(double a):向上取整
public static double floor(double a):向下取整
public static int max(int a,int b): min自学
public static double pow(double a,double b):a的b次方
public static double random():随机数。[0,1)
public static int round(float a): 四舍五入
public static double sqrt(double a):求平方根
Random类:
public Random():无参构造,默认以当前时间为种子。
public Random(long seed):给定种子的构造方法.给定种子后,每次得到的随机数是相同的
public int nextInt():返回一个int范围内的随机数。
public int nextInt(int n):返回[0,n)的随机数。
public static int getRandom(int start, int end) {//产生一个范围内的随机数
int number = (int)(Math.random()*(end-start+1)) + start;
return number;
}
Character类:
public static boolean isUpperCase(char ch):判断一个字符是否是大写字母
public static boolean isLowerCase(char ch):判断一个字符是否是小写字母
public static boolean isDigit(char ch):判断一个字符是否是数字
public static char toUpperCase(char ch):转成大写字母
public static char toLowerCase(char ch):转成小写字母
座右铭:站在别人的思想上,看见自己的不足,传播错误的经验,愿君不重蹈覆辙。
由于受限于本人经验,难免不足,如有建议,欢迎留言交流。
说明:如果喜欢,请点赞,您的鼓励是本人前进最好的动力