星期四, 十一月 05, 2015 ? 16:18:52
?
3版 ?142---176
?
?
一.创建字符串
??
在java中用String类的构造方法来创建字符串变量。
?
1.String()
? ? ? ? ?
? ? ? ? ? ? 一个String对象,可表示一个空字符序列。
? ? 使用String()方法初始化一个新创建的String对象
? ? String s = ?new String();
?
?
二.字符串的操作
?
1.字符串连接
? ?
? ? ? ? a.多个字符串的连接
?
代码案例:
package day05;
?
public class TestString {
public static void main(String[] args) {
/*字符串连接用+*/
int m = 20;
String s1 = new String("hello");
String s2 = new String("word");
/*String s = ""+s1+s2;*/
System.out.println("m先转换为字符串-->连接"+m+s1+s2);
}
}
?
运行结果:
?
m先转换为字符串-->连接20helloword
?
?
2.连接其他数据类型
?
?
当其他数据类型与字符串相连时会自动将其转换成字符串类型,然后进行连接。
?
?
3.获取字符串信息
?
? ? 字符串变量是一个对象,可以通过相应方法获取字符串的有效信息,如获取字符串的长度、获取指定字符的索引位置等。
? ? ----1.获取字符串的长度
? ? ----2.获取指定字符串的索引位置,首次出现的str.indexOf(int ch)
? ? ? ? ? &最后一次出现的lastIndexOf(int ch)
? ? ----获取指定索引位置的字符
?
代码案例:
package day05;
?
public class TestString {
public static void main(String[] args) {
/*获取字符的指定的索引位置indexOf(int ch)
* lastIndexOflastIndexOf*/
String s = new String("hwxzwkf");
int loc = s.indexOf(‘w‘);
int loc2 = s.lastIndexOf(‘w‘);
/*charAt(int index)*/
char m = s.charAt(1);
System.out.println("w在hwxzkf中首次出现的位置="+loc);
System.out.println("w在hwxzkf中last出现的位置="+loc2);
System.out.print("索引位置为1的字符为"+m);
}
}
运行结果:
?
w在hwxzkf中首次出现的位置=1
w在hwxzkf中last出现的位置=4
索引位置为1的字符为w
?
4.去掉字符串的空格
?
---去掉字符串中的空格有2种,1.去除字符串的前导、尾部空格,2.去除字符串中的所有空格,可以用不同方法实现。
? ??
? ? 1.trim()进行去除字符串的前导、尾部空格,
?
? ? 2.replaceAll(String regex,
?String replacement),去除字符串中的所有空格。
?
? ? http://blog.csdn.net/zhangjg_blog/article/details/18319521
?
? ? ? ?String是不可以改变解释~~
?
? ? ? -----在Java中不可能直接操作对象本身,所有的对象都由一个引用指向,必须通过这个引用才能访问对象本身,
? ? ? 包括获取成员变量的值,改变对象的成员变量,调用对象的方法等。
?
? ? ? -----也就是说,s只是一个引用,它指向了一个具体的对象,当s=“123456”; 这句代码执行过之后,又创建了一个新的对象“123456”, 而引用s重新指 ? ? ? 向了这个心的对象,原来的对象“ABCabc”还在内存中存在,并没有改变。
? ? ? -----原值在内存中还是在的;新的赋值只是将新的数据在内存中新生成,将s重新进行了指向。
?
代码案例:
?
package day05;
?
public class TestString {
public static void main(String[] args) {
String s = " ?J ava ";
System.out.println("字符串中含空格的长度="+s.length());
System.out.println("字符串中使用trim去除前后空格后的长度,中间的空格未去除"+s.trim().length());
System.out.println(s=s.trim());
?
/*public String replaceAll(String regex,
? ? ? ? ? ? ? ? String replacement)*/
?
/*s.replaceAll(" ", "");*/
System.out.println("原s的指向="+s);
System.out.println("s的重新指向="+s.replaceAll(" ", ""));
}
}
?
运行结果:
? ??
字符串中含空格的长度=8
字符串中使用trim去除前后空格后的长度,中间的空格未去除5
J ava
原s的指向=J ava
s的重新指向=Java
?
?
5.字符串替换
?
? ? ----replace(char oldChar,char newChar)?
? ? ? ? ? ? 该方法用户替换所有与指定字符串相匹配的字符串
?
? ? ----replaceFirst(String regex,String replacement)
? ? ? ? ? ? 该方法用于替换第一个出现的指定的字符串,而后面出现的指定字符串则不会被替换
?
代码案例:
package day05;
?
public class TestString {
public static void main(String[] args) {
/*5.字符串替换public String replace(char oldChar,
? ? ? ? ? ? ?char newChar)*/
String a = new String("bad bad girl");
a = a.replace("bad", "good");
System.out.println("repalce使用,a的新指向"+a);
?
?
/*public String replaceFirst(String regex,
? ? ? ? ? ? ? ? String replacement)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?参数:
? ? ? ? ? ? ? ?regex - the regular expression to which this string is to be matched
? ? ? ? ? ? ? ?replacement - the string to be substituted for the first match*/
String b = new String("bad bad girl");
b = b.replaceFirst("bad", "good");
System.out.println("replaceFirst使用,b的新指向"+b);
?
?
}
}
?
运行结果:
repalce使用,a的新指向good good girl
replaceFirst使用,b的新指向good bad girl
?
星期四, 十一月 05, 2015 19:54:50
?
?
?
原文:http://yuzhouxiner.iteye.com/blog/2254766