目录
2 、public char charAt(int n)和public void setCharAt(int nchar ch)
3 、StringBuffer insert(int index,String str)
4、public StringBuffer reverse()
5、StringBuffer delete(int startIndex, int endIndex)
6、StringBuffer replace( int startIndex, int endIndex, String str)
String类
由于在程序设计中经常涉及处理和字符序列有关的算法,为此Java专门提供了用来处理字符序列的String类。String类在java.lang包中,由于javalang包中的类被默认引入,因此程序可以直接使用String类。需要注意的是Java把String类定义为final类, 扫一扫 因此用户不能扩展String类,即String类不可以有子类。
StringBuffer类的常用方法
1、append方法
StringBuffer append(String s):将 String对象s的字符序列追加到当前 StringBuffer 对象的字符序列中,并返回当前 StringBuffer对象的引用。
StringBuffer append(intn):将int型数据n转化为 String 对象,再把该 String 对象的字符序列追加到当前StringBuffer 对象的字符序列中,并返回当前StringBuffer 对象的引用。
StringBuffer append(Object o):将一个Object 对象o的字符序列表示追加到当前 String Buffer 对象的字符序列中,并返回当前 StringBuffer 对象的引用。
类似的方法还有StringBuffer append(longn)、StringBuffer append(boolean n)、StringBuffer append(float n)、StringBuffer append(doublen)和StringBuffer append(char n)。
2 、public char charAt(int n)和public void setCharAt(int nchar ch)
char charAt(int n)得到StringBuffer对象的字符序列位置n上的字符。setCharAt(int n, char ch)将当前StringBuffer对象的字符序列位置n处的字符用参数ch指定的字符替换(n的值必须是非负的,并且小于当前对象实体中字符序列的长度,StringBuffer 对象的字符序列的第一个位置为0,第二个位置为 1,依次类推)。
3 、StringBuffer insert(int index,String str)
StringBuffer 对象使用insert方法将参数str指定的字符序列插入到参数index指定的位置,并返回当前对象的引用。
4、public StringBuffer reverse()
StringBuffer 对象使用 reverse()方法将该对象实体中的字符序列翻转,并返回当前对象的引用。
5、StringBuffer delete(int startIndex, int endIndex)
delete(int startlndex, int endIndex)从当前 StringBuffer对象的字符序列中删除一个子字符序列,并返回当前对象的引用。删除的子字符序列由下标 startIndex 和 endIndex指定:从 startIndex位置到 endIndex-1位置处的字符序列被删除。deleteCharAt(int index)方法删除当前 StringBuffer 对象实体的字符序列中index位置处的一个字符。
6、StringBuffer replace( int startIndex, int endIndex, String str)
replace( int startIndex, int endIndex, String str)将当前 StringBuffer 对象的字符序列的一个子字符序列用参数 str指定的字符序列替换。被替换的子字符序列由下标 startIndex和endIndex指定,即从 startIndex 到 endIndex-1 的字符序列被替换。该方法返回当前StringBuffer 对象的引用。
public class Example1{
public static void main(string args[]) {
StringBuffer str=new StringBuffer();
str.append("大家好");
System.out.println("str:"+str);
System.out.println("length:"+str.length());
System.out.println("capacity:"+str.capacity());
str.setCharAt(0 ,'w');
str.setcharAt(1 ,'e');
System.out.println(str);
str.insert(2, " are all");
System.out.println(str);
int index=str.indexOf("好");
str.replace(index,str.length()," right");
System.out.println(str);
}
}
输出
str:大家好
length:3
capacity:16
we好
we are all好
we are all right
Date类与Calendar类
(1)Date类
无参数构造法
Date nowTime = new Date();
System.out.println(nowTime);
输出
Fri Mar 31 23:45:33 CST 2023
带参数构造方法
Date date=new Date(1000),
date2=new Date(-1000);
其中的参数取正数表示公元后的时间,取负数表示公元前的时间,例如1000表示1000毫秒,那么,date1含有的日期、时间就是计算机系统公元后1秒时刻的日期、时间。如计算机系统将自身的时间的“公元”设置在2023年1月1日0时(格林威治时间),那么date1就是2023年01月01日08时00分01秒,date2就是2023年01月01日07时59分59秒(北京时间与格林威治时间相差8个小时)。
(2)Calendar类
Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为静态成员变量,方便获取。
Calendar与Date的转换:
public class CalendarTest {
public static void main(String[] args) {
//Calendar--->Date
Calendar c = Calendar.getInstance();
Date d = c.getTime();
//Date--->Calendar
Date d1 = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
System.out.println(d);
System.out.println(c1.get(Calendar.YEAR)+"年"+(c1.get(Calendar.MONTH)+1)+"月"+c1.get(Calendar.DATE)+"日");
}
}
运行结果
Sat Apr 01 00:19:14 CST 2023
2023年4月1日
Math类和Random类
(1)Math类
在编写程序时,可能需要计算一个数的平方根、绝对值或获取一个随机数等。java.lang包中的Math 类包含许多用来进行科学计算的static 方法,这些方法可以直接通过类名调用。另外,Math类还有两个static常量:E和PI,二者的值分别是2.7182828284590452354和3.14159265358979323846。
以下是Math类的常用方法:
public static long abs(double a)\\返回 a 的绝对值。
public static double max(double a,double b)\\返回a、b 的最大值。
public static double min(double a,double b)\\返回 a、b 的最小值。
public static double random()\\产生一个0~1之间的随机数(包括0不包括 1)。
public static double pow(double a,double b)\\返回 a 的 b 次幂。
public static double sqrt(double a)\\返回a的平方根。
public static double log(double a)\\返回a的对数。
public static double sin(double a)\\返回a的正弦值。
public static double asin(double a)\\返回 a 的反正弦值。
public static double ceil(double a)\\返回大于 a 的最小整数,并将该整数转化为 double型数据(方法的名字ceil 是天花板的意思,很形象)。例如,Math.ceil(15.2)的值是16.0。
public static double floor(double a)\\返回小于 a 的最大整数,并将该整数转化为 double型数据。例如,Math.floor(15.2)的值是15.0,Mathfloor(-15.2)的值是-16.0。
public static long round(double a)\\返回值是(long)Math.floor(a+0.5)),即所谓a的“四舍五入”后的值。一个比较通俗好记的办法是:如果a是非负数,round 方法返回 a的四舍五入后的整数(小数大于等于0.5入,小于0.5舍);如果a是负数,round方法返回a的绝对值的四舍五入后的整数取负,但注意,小数大于0.5入,小于等于 0.5舍,例如,Math.round(-15.501)的值是-16,Math.round(-15.50)的值是-15。
(3)Random类
返回一个随机整数
Rondom random = new Random();
random.nextInt();
返回 [0,99) 之间的整数
random.nextInt(100);
返回一个随机 boolean 值
random.nextBoolean();