常用方法
Integer.parseInt(obj):将obj转换成int类型(同理于double等) Integer.parseInt(obj,8):将obj转int,以8进制形式 a.compareTo(b):a与b比较大小,a大返回1,b大返回-1,相等返回0
Integer i = Integer.valueOf("1111",2);
System.out.println(i.intValue());
System.out.println(Integer.toBinaryString(17));
System.out.println(Integer.toOctalString(17));
System.out.println(Integer.toHexString(17));
Character包装类
Character c = new Character('A');
Character cStatic = Character.valueOf('F');
char cInval = cStatic.charValue();
System.out.println(cInval);
System.out.println(cStatic.compareTo(c));
System.out.println(cStatic.toString().getClass().getName());
System.out.println(Character.isLetter('q'));
System.out.println(Character.isLowerCase('f'));
System.out.println(Character.isUpperCase('D'));
System.out.println(Character.isDigit('2'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isDefined(-1));
system实用方法
package day13Test;
import java.util.Properties;
public class SystemTest {
public static void main(String[] args) {
testOut(-23);
System.out.println(System.currentTimeMillis());
System.gc();
Properties m = System.getProperties();
System.out.println(m);
}
public static void testOut(int age){
if(age<0){
System.err.println("年龄不合法");
}else{
System.out.println("年龄合法");
}
}
}
结果: 
字符串String及其常用方法
string类型是以常量存储,值创建之后不能改变
char[] chs = new char[]{'2','e','t','y','P'};
String s = new String("你好");
String s1 = new String(chs);
String s2 = new String(chs,0,3);
System.out.println(s1);
System.out.println(s2);
比较:用string.equal(string1)方法,对比的是值,而不是地址
String s = "小明";
String s1 = "小明";
System.out.println(s.equals(s1));
System.out.println(s.equals(new String("小明")));
System.out.println(s == s1);
System.out.println(s== (new String("小明")));
string.charAt(int)查找字符串中的位置,返回char
String s = "我爱我的祖国";
System.out.println(s.charAt(1));
表示以…开头/结尾 string.startsWith(s1);返回boolean string.endsWith(s2);返回boolean
String s = "我爱我的祖国";
System.out.println(s.endsWith("祖国"));
System.out.println(s.startsWith("我爱我"));
string.trim()去掉前后的空格
String m = " sd df sdf ";
System.out.println(m.trim());
System.out.println(m);
string.indexOf(string s1) / string.indexOf(String s1,int index) 查找指定字符串的位置/从坐标index开始查找字符串的位置 找不到返回-1
String s1 = "sdfdfihknjksbgkjbk";
System.out.println(s1.indexOf("k"));
System.out.println(s1.indexOf("k",8));
String[] split(String regex)分割字符串,返回分割后的字符串数组
String s = "你好,我是天下最笨的人,你是GG还是MM,天天都要做题?";
String[] sArray = s.split("天");
System.out.println(sArray[0]);
System.out.println(sArray[1]);
System.out.println(sArray[2]);
System.out.println(sArray[3]);
string.substring(int index)/string.substring(int start,int end)返回index后面的字符串/返回start和end之间的字符串
String texts = "我爱洗衣服,天天晒不干";
System.out.println(texts.substring(1));
System.out.println(texts.substring(3,7));
char[] toCharArray()将String转成char[],返回一个char[](原值不变)
String s = "0123456789";
char[] chs = s.toCharArray();
System.out.println(chs[1]);
string.toLowerCase()转小写(全部) string.toUpperCase()转大写(全部)
String s = "sdfHIOsdHjhHo";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
string.replace(旧字符/字符串,新字符/字符串) 用新的取代旧的,返回一个string结果
String s = "sdnsdonii";
System.out.println(s.replace("s","天边"));
String.valueOf(Object obj)转成Stirng类型,返回String类型的数据
System.out.println(String.valueOf(23) + 78);
StringBuilder
创建
StringBuilder builder = new StringBuilder("我爱我的国家");
System.out.println(builder);
常用方法 StringBuilder拥有String类的大多数方法,但也拥有可以对字符串修改的类 参考:this append(String str)/append(Char c)字符串连接
System.out.println(new StringBuilder("abcd").append("efg"));
toString():返回一个与构建起或缓冲器内容相同的字符串
StringBuilder sb = new StringBuilder("this is a pen");
System.out.println(sb.toString());
setCharAt(int i, char c):将第 i 个代码单元设置为 c(可以理解为替换)
StringBuilder sb = new StringBuilder("aaaaaa");
sb.setCharAt(1,'A');
System.out.println(sb);
insert(int offset, String str)/insert(int offset, Char c):在指定位置之前插入字符(串)
StringBuilder sb = new StringBuilder("a dog");
sb.insert(2,"hot");
System.out.println(sb);
delete(start,end)删除索引start到end的字符串
StringBuilder sb = new StringBuilder("0123456789");
System.out.println(sb.delete(0,7));
deleteChatAt(int index)删去指定位置的字符
StringBuilder sb = new StringBuilder("0123456789");
System.out.println(sb.deleteCharAt(1));
replace(start,end,新字符串) 在start和end之间用新字符串取代
StringBuilder sb = new StringBuilder("0123456789");
System.out.println(sb.replace(1,2,"中文"));
stringBuilder.reverse()反转
StringBuilder sb = new StringBuilder("012");
System.out.println(sb.reverse());
void setLength(int)设置stringBuilder的长度
StringBuilder sb = new StringBuilder("012weewrew");
sb.setLength(3);
System.out.println(sb);
使用以上这些方法需要注意,用完方法后是直接修改了stringBuilder对象的值,而不产生新的对象
|