码迷,mamicode.com
首页 > 编程语言 > 详细

java面向对象:String类的用法

时间:2015-07-25 22:59:00      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:java

package com.cloud.oop;


public class Demo2 {


/**
* @param args
*/
public static void main(String[] args) {
System.out.println("===String类型:实例化String类对象===");
String name="Tom";
System.out.println("name="+name);
String name1=new String("Cat");
System.out.println("name1="+name1);
System.out.println("===‘==‘比较字符串地址===");
String str1="hello";
String str2=new String("hello");
String str3=str2;
System.out.println(str1==str2);
System.out.println(str1==str3);
System.out.println(str2==str3);
System.out.println("===equals比较字符串内容===");
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println(str2.equals(str3));
System.out.println("===字符串的内容不可改变===");
//这里是通过内存地址的断开,连接实现变化的

String str4="hello";
str4=str4+"China";
System.out.println(str4);
System.out.println("===String类的常用方法:toCharArray===");
String strch="hello";
char c[]=strch.toCharArray();
for(int i=0;i<c.length;i++){
System.out.print(c[i]+"\t");
}
System.out.println("");
String strch1=new String(c);
String strch2=new String(c, 0, 3);
System.out.println(strch1+"\n"+strch2);
System.out.println("===从字符串中取指定位置===");
String sptr="hello";
System.out.println(sptr.charAt(4));
System.out.println("===和byte数组之间相互转换===");
String bystr="hello";
byte b[]=bystr.getBytes();
System.out.println(new String(b));
System.out.println(new String(b,1,3));
System.out.println("===判断字符串是否存在===");
String instr="dsihsadhald";
System.out.println(instr.indexOf("l"));
System.out.println(instr.indexOf("d", 4));
System.out.println("查询不到返回:"+instr.indexOf("e"));
System.out.println("===去掉字符串两边的空格===");
String trstr="  hell o  ";
System.out.println(trstr.trim());
System.out.println("===去掉所有空格===");
String str7="   h e  l l o  ";
System.out.println(str7);
String str8[]=str7.split(" ");
for(int i=0;i<str8.length;i++){
if(str8[i]==" "){
return;
}else{
System.out.print(str8[i]);
}
}

}


}

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

java面向对象:String类的用法

标签:java

原文地址:http://blog.csdn.net/dzy21/article/details/47061151

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!