黑马程序员---String 类

-------android培训java培训、期待与您交流! ----------

1.字符串是特殊的对象
2。初始化就不可改变

class StringDemo
{
   public static void main()
   {
      String s1 ="abc";
      // s1是类型变量,"abc" 是对象

      String s2 = new String("abc");

      String s3 = "abc";

      System.out.println(s1==s3);     //true
      System.out.println(s1==s2);    //等号比较地址  false
      System.out.println(s1.equals(s2));  //判断字符串值是否相等  true  
   }
}


/*
S1和S2 的区别
s1 在内存中有一个对象
s2在内存中有两个对象
*/


//String包含的方法

class StringDemo
{
    public static void meth_get()
  {
    String str = "aaaaa";

    //求长度
    sop(str.length());

    //根据索引取获取字符
    sop(str.charAt(3));    //IndexOutOfBoundsException - 如果 index 参数为负或小于此字符串的长度。


    //根据字符获取索引
    sop(str.indexOf('a'));   //没有找到就返回-1;
   }
   public static void main(String args[])
   {
       meth_get();
   }
  
   public static void sop(Object obj)
   {
       
   }
}


《===================String 操作字符,字节====================》

1@字节数组转换字符串

 char ch = {'I','m','i'};

 String s = new String(ch);

 String s1 = new String(ch,1,2);


1@toCharArray将此字符串转换为一个新的字符数组

String  s1 = "abcdefg";
 
char  ch[]   =  s1.toCharArray();

for(int i = 0;i<ch.length;i++)
{
    sop(ch[i]);
}

3@replace返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的

public String replace(char oldChar,char newChar)

String s = "abch";
 
String s1  =  s.replace('a','c');

String s2  =  s.replace("abc","cba"); //"hcba"


4@public String[] split(String regex)
//根据匹配拆分字符串

String  s  = "abc,cba,ccc";

String s1[]  =  s.split(",");

for(int i = 0;i<s1.length;i++)
{
   sop(s1[i]);
}


5@返回一个新的字符串,它是此字符串的一个子字符串

String s = "abcdefghei";

String s1  = s.substring(2);
String  s2  = s.substring(2,4);

System.out.println(s1+s2);


6@(toUpperCase)使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

(toLowerCase)使用默认语言环境的规则将此 String 中的所有字符都转换为小写

String s =  "aaaaaaaaaaa";
String s1  = s.toUpperCase();


String  s2  = "AAAAASLFKAL";
String  s3  =  s2.toLowerCase();

7@返回字符串的副本,忽略前导空白和尾部空白。但是中间的去不掉

public String trim(){}

String  s = "   a a   ";

String   s1  = s.trim();

8@按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值


String s  = "aaaslsfls";
String s1  =  "aaaoiiio";

//对两个字符串进行自然比较
int compareTo (String);
int a = s.compareTo(s1);

 

字符串练习


public static void main()
{
   int count =  0;
   int index  = 0;
   while(((index=str.indexof(key))!-1)
   {
       str = str.substring(index+key.length());
      
       count++;
   }
   return count;
}

<===============StringBuffer 字符串缓冲区===================>

class StringBufferDemo
{
    public static void main()
    {
       StringBuffer sb = new  StringBuffer();
       sb.append("aa").append(ture).append(12);
       StringBuffer   sb1 =  sb.append(23);

    }
}


<======================例题=======================>

class ABC {
 public static void main(String args[]) {
  Collection<String> c=new ArrayList<String>();
  Collection<String> c2=new ArrayList<String>();
  c.add("I");
  c.add("Love");
  c.add("you");
  c.add(",");
  System.out.println(c);
  c2.addAll(c);
  c2.add("中国");
  c.add("!");
  System.out.println(c2);
  Object[] e = new String[c2.size()];
  e = c2.toArray();
  for(int i=0;i<e.length;i++){
   System.out.println(e[i]);
  }
 }
}

toArray()方法返回的是一个Object数组,你想把String放到object数组的话应该得向上转型。Object[] e = new String[c2.size()];这样。


 

 

-------android培训java培训、期待与您交流! ----------

详细请查看:http://edu.csdn.net/heima/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值