String的转换:
Example03.java:
public class Example03{
public void static main(String[] args){
String str = "absssss";
Char[] c = str.toCharArray();
for(int i = 0; i <= c.length -1 ; i ++ ){
if(i != c.length - 1 ) {
System.out.print(c[i] +",");
}else{
System.out.print(c[i]);
}
}
System.out.println(String.valueOf(11));
System.out.println(str.toUpperCase());
}
String的替换和去除空格操作:
public class Example05{
public void static main(String[] args){
String str = "itcast";
System.out.println(str.replace("it","cn.it");
String str1 = "i t c a s t "
System.out.println(str1.trim
);
System.out.println(str1.replace(" ","");
}
}
字符串的判断:
public class Example06{
public void static main(String[] args){
String s1 = "String";
String s2 = "Str";
System.out.println(s1.endsWith("ng"));
System.out.println(s1.startsWith("S"));
System.out.println(s1.contains("tri"));
System.out.println(s1.isEmpty());
System.out.println(s1.equals(s2));
}
}
字符串的截取和分割:
public class Example07{
public void static main(String[] args){
String s = "Peter-Morton-Ben";
System.out.println(s.substring(3));
System.out.println(s.substring(3,9));
String[] stringArray = s.split("-");
for(int i = 0; i < stringArray.length ; i ++){
if(i != (stringArray.length - 1)){
System.out.print(stringArray[i]+",");
}else{
System.out.print(stringArray[i]);
}
}
}
}
Runtime类:
用于封装jvm虚拟机进程
采用单例模式,不可以直接实例化:
Runtime runTime = Runtime.getRuntime();
常用方法:
int availableProcessors()
int freeMemory()
int maxMemory()
Process exec()---执行一个电脑进程(类似cmd)
Random类:
两种实例方法:
Random r = new Randow()---无种子
Random r = new Randow(10)---有种子
其差别在于:如果指定了相同的种子,则每个实例对象产生的随机数具有相同的序列
public class Example16{
public void static main(String[] args){
Random r = new Random(); //创建对象时不传入种子
for(int i = 0 ; i < 10 ; i ++){
System.out.println(r.nextInt(1000));
}
}
}
public class Example17{
public void static main(String[] args){
Random r = new Random(); //创建对象时传入种子
for(int i = 0 ; i < 10 ; i ++){
System.out.println(r.nextInt(1000));
}
}
}
Randow的常用方法:
boolean nextBoolean()
double nextDouble()
float nextFloat()
int nextInt()
int nextInt(int n)
long nextLong()
SimpleDateFormat类的实例化直接new:
SimpleDateFormat sdf = new SimpleDateFormat();
Calendar类的总要方法:
int get(int field)
void add(int field , int amount)
void set(int field , int value)
void set(int year , int month , int date)
void set(int year, int month , int date , int hourOfDay, int minute, int second)
三者比较:
如果要到达一个统一的目的(即格式化时间)
public class Calender {
public void static main(String[] args){
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
public class DateFormat {
public void static main(String[] args){
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); //参数有以下几种:DateFormat.FULL,DateFormat.LONG,DateFormat.MEDIUM,DateFormat.SHORT
Date date = new Date();
String time = df.format(date);
System.out.println(time);
}
}
public class SimpleDateFormat {
public void static main(String[] args){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("Gyyyy年MM月dd日:今天是yyyy年的第D天,E");
String time = sdf.format(date);
}
}
public class Example30 {
public void static main(String[] args){
String week = "friday";
switch(week){
case "monday":
System.out.println("星期一");
break;
case "Tuesday":
System.out.println("星期二");
break;
case "Wednesday":
System.out.println("星期三");
...
}
}