package com.cxy.e;
public class StringTest
{
/**
* String类
*/
public static void main(String[] args)
{
//String.valueOf 将各种类型转换成String类型
int a=123;
String aStr=String.valueOf(a);
System.out.println("================================");
//字符串的长度
String name="chenxiaoyang";
System.out.println(name.length());
System.out.println("================================");
//字符串比较
String aa=new String("cxy");
String bb=new String("cxy");
String cc="cxy";
String dd="cxy";
if(aa.equals(bb))
{
System.out.println("aa和bb的值相同");
}
if(aa==bb)
{
System.out.println("aa和bb的引用相同");
}else
{
System.out.println("aa和bb的引用不相同");
}
System.out.println("================================");
//字符串比较忽略大小写
String aa1=new String("cxy");
String bb2=new String("Cxy");
if(aa1.equalsIgnoreCase(bb2))
{
System.out.println("aa和bb的值相同(忽略大小写)");
}
if(aa1.equals(bb2))
{
System.out.println("aa和bb的值相同(不忽略大小写)");
}else
{
System.out.println("aa和bb的值不相同(不忽略大小写)");
}
System.out.println("================================");
//比较两个字符串 比大小
String aa2=new String("123");
String bb3=new String("1011");
if(aa2.compareTo(bb3)<0)
{
System.out.println("aa2在aa3的前面");
}else if(aa2.compareTo(bb3)==0)
{
System.out.println("aa2和aa3相等");
}else if(aa2.compareTo(bb3)>0)
{
System.out.println("aa2在aa3的后面");
}
System.out.println("================================");
//判断开始和结束
System.out.println("hello world".startsWith("hello"));
System.out.println("hello world".endsWith("ld"));
System.out.println("================================");
//搜索字符串
System.out.println("a,b,c,d,e,f".indexOf(","));
System.out.println("a,b,c,d,e,f".lastIndexOf(","));
System.out.println("a,b,c,d,e,f".lastIndexOf("@"));
System.out.println("================================");
//字符串截取
System.out.println("I like java api".substring(7));
System.out.println("I like java api".substring(7,11));
System.out.println("================================");
//替换
String tempTestStr="I like java api";
System.out.println(tempTestStr.replace("java", "c++"));
System.out.println(tempTestStr);
System.out.println("================================");
//去掉前后空格
System.out.println(" I like java api ".trim());
System.out.println("================================");
//改变大小写
System.out.println("java api".toUpperCase());
System.out.println("JAVA API".toLowerCase());
System.out.println("================================");
//一个非常有用的方法
String[] strArray="a,bdfsgf,c,d,e,f".split(",");
for(String one :strArray)
{
System.out.println(one);
}
System.out.println("================================");
}
}