黑马程序员技术交流社区
标题:
java学习之字符串
[打印本页]
作者:
至简_HREO
时间:
2015-6-30 07:42
标题:
java学习之字符串
1.较大的字串提取一个子串
String greeing = "hello";
String s = greet.substring(0,3);
创建一个由字符“hel”组成的字符串
2.字符串拼接
String expletive = "Expletive";
string PG13 = "deleted";
String message = expletive + PG13;
上述代码将“Expletivedeleted”赋给变量message(注意,单词之间没有空格,+号按照给定的次序将两个字符串拼接起来)
3.检查字符串是否相等
可以使用equals方法检查两个字符串是否相等。例如:
str1.equals(str2)
如果字符串str1与字符串str2相等,则返回true,否则返回flase.
若字符串为常量时:
"hello".equals("hello")
上述方式也是合法的。
若想检测两个字符串是否相等,而且忽略大小写时,可以用equalsIgnoreCase方法.
"Hello".equalsIgnoreCase("hello");
注意:
•一定不能用==运算符检测两个字符串是否相等!这个运算只能确定两个字符串是否放置在同一个位置上。
•+与substring等操作产生的结果并不是共享的。
4.空串与Null串
空串""是长度为0的字符串.
检查一个字符串是否为空的代码:
if(str.length()==0)
或
if(str.equals(""))
作者:
luorenyu
时间:
2015-6-30 09:04
这里还没有学过
作者:
fantianfei
时间:
2015-6-30 09:50
谢谢分享。。。
作者:
wenxueaaa555
时间:
2015-6-30 19:52
public class StringDemo {
public static void main(String[] args) {
//判断
String s="abcde";
System.out.println("startsWith(String str):"+s.startsWith("ab"));//true
System.out.println("endsWith(String str):"+s.endsWith("e"));//true
System.out.println("contains(String str):"+s.contains("cd"));//true
System.out.println("isEmpty():"+s.isEmpty());//false
System.out.println("equals(String str):"+s.equals("AbCde"));//false,区分大小写
System.out.println("equalsIgnoreCase(String str):"+s.equalsIgnoreCase("AbCde"));//true,不区分大小写
//获取
String str="abcddfefd";
System.out.println("str.length():"+str.length());//9
System.out.println("str.indexOf(int ch):"+str.indexOf('f'));//5
System.out.println("str.indexOf(String str):"+str.indexOf("d"));//3
System.out.println("str.lastIndexOf(int ch):"+str.lastIndexOf('f'));//7
System.out.println("str.lastIndexOf(String str):"+str.lastIndexOf("d"));//8
System.out.println("str.charAt(int index):"+str.charAt(0));//a
System.out.println("str.substring(int beginIndex):"+str.substring(2));//cddfefd
System.out.println("str.substring(int beginIndex,int endIndex):"+str.substring(3, 5));//dd
//转换
String str1="HelloWorld";
byte[] byt=str1.getBytes();
for (int i = 0; i < byt.length; i++) {
System.out.print(byt[i]+" ");//72 101 108 108 111 87 111 114 108 100
}
System.out.println();
char[] chs=str1.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.print(chs[i]+" ");// H e l l o W o r l d
}
System.out.println();
System.out.println("String.valueOf(int i)基本类型:"+String.valueOf(123));//123
char[] ch={'a','b','c'};
System.out.println("String.valueOf(char[] data):"+String.valueOf(ch));//abc
System.out.println("str1.toUpperCase():"+str1.toUpperCase());//HELLOWORLD
System.out.println("str1.toLowerCase():"+str1.toLowerCase());//helloworld
//替换
String str2=" hello----world ";
System.out.println("str2.replace(char old.char new):"+str2.replace('o', 'O'));// hellO----wOrld
System.out.println("str2.trim():"+str2.trim());//hello----world
String[] strs=str2.trim().split("-");
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i]+" ,");//hello, , , ,world,
}
System.out.println();
System.out.println(strs.length);//5
}
}
作者:
ZhangHeng
时间:
2015-6-30 22:14
谢谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2