JAVA基础练习 字符串
1.输出控制台传递的默认参数
public clasa work1(){
public static viod main(String[] args){
System.out.println(args);
}}
2.比较字符串
String str = “xx”;
String str1 = “yy”;
str.compareTo(str1)
str.compareToIgnoreCase(str1)
equals.
3.确定指定的子字符串在字符串内的最后出现位置
strOrig.lastIndexOf(Stringname)
String strOrig = “xyz";
int lastIndex = strOrig.lastIndexOf(x);
4.从字符串删除指定位置的字符
removeCharAt(string,position)
5.用一个字符串替换另一个字符串中的字串
replace()
6.字符串反转
String类型的每次改动是生成一个新的String对象
StringBuffer是对StringBuffer本身进行操作
反转:StringBuffer(string).reverse().toString().
7.在字符串中搜索词组所在的位置
String strOrig = “hello”
int intIndex = strOrig.indexof(“e”);
8.将字符串拆分 split(string)
String str=“this-is-me”;
String[] temp;
temp = str.split(-);
for(int i = 0; i < temp.length(); i++){
System.out.println(temp[i]);
}
9.将字符串全部转换为大写
str.toUpperCase();
10.匹配字符串中的区域
regionMatches();
boolean match = str1.regionMatches(11,str2,12,3)
11.连接两个字符串
“+” append()
StringBuffer result = new StringBuffer();
result.append(yin)
字符串的方法
1.compareTo,compareToIgnoreCase,==,equals
2.lastIndexOf
3.removeCharAt(str,position)
4.replace(a,b)
5.String str = new StringBuffer(string).reverse().toString()
6.int i = str.indexof(e)
7.str = “x-y-z”
String[] temp;
temp = str.split("-");
for(i=0;i<temp.length;i++){
System.out.println(temp[i]);
}
8.str.toUpperCase
9.regionMatches
返回boolean值
boolean match = str1.regionMatches(0,str2,1,5)
- StringBuffer result = new StringBuffer();
result.append(yin);