String的indexOf(),lastIndexOf(),substring(int x),substring(int x , int y)方法 StringBuffer

  String 的 indexOf()与lastIndexOf()方法返回的int整数类型的数据;

                substring(int x),substring(int x , int y)返回的是String类型的字符串。

 

             具体如下:

     indexOf("char ");若指定字符串中没有char字符则返回-1

     indexOf("char ", n); 从第n个字符位置开始往后继续查找char ,(包含当前位置),返回找到char后的索引

     lastIndexOf("char ");  从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定字符的索引



     substring(int x);该方法是获得将原字符串从头开始截取长度为x的字符后剩余的新字符串。

    substring(int x , int y);该方法是获得从原字符串截取索引从x到y(不含y)的字符(包含x不包含y的组成字符串片段。







http://blog.csdn.net/m0_37908260/article/details/70599151

StringBuffer对象和String对象之间的互转的代码如下:

    String s = “abc”;

StringBuffer sb1 = new StringBuffer(“123”);


StringBuffer sb2 = new StringBuffer(s);      //String转换为StringBuffer

String s1 = sb1.toString();                         //StringBuffer转换为String



package springTest2;


public class Test {

public static void main(String[] args) {
String s="fidSStrr.idg";
  String a = new String("hello");
  //像a=new string("hello");创建了a对象好像就不能对这个字符串对象操作了,变成"hell"就是a指向另外一个对象了
String a1=a.substring(0, 4);
System.out.println(a1);//hell
System.out.println("a与a1是否相等:"+(a==a1));//false 
  
  StringBuffer b=new StringBuffer("world");
  
    StringBuffer b1 = b.deleteCharAt(4);     //存在继承关系且属于同一类型,才可以进行强转                 

      
      String b2 = b1.toString(); //将StringBuffer转化成  String,相当于隔开StringBuffer的内存空间,又创建了一个新的内存空间存String对象
      
      System.out.println("String类型b2的地址值:"+(b2.hashCode()));
      System.out.println("StringBuffer类型b的地址值:"+(b.hashCode()));
      System.out.println(b2==(b.toString()));//String类型与StringBuffer类型不是同一个对象,内存地址值空间不同
   /**
    * 用StringBuffer操作字符串,不会创建新的字符串对象,还是在原来的字符串上进行修改。
    */
      System.out.println(b1);
    System.out.println(b1.hashCode()+"------------"+b.hashCode());//修改后的字符串对象地址值没有改变,还是原来的字符串对象。
    System.out.println("b1与b是否相等:"+(b==b1));
   
//从头开始查找是否存在指定的字符
System.out.println(s.indexOf("d"));//2

//从第4个字符位置开始往后继续查找S,包含当前位置
System.out.println(s.indexOf("S", 3));//3

//若指定字符串中没有该字符则返回-1
System.out.println(s.indexOf("0"));//-1

//从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定字符的索引
System.out.println(s.lastIndexOf("r"));//7


//substring(int x);该方法是获得将原字符串从头开始截取长度为x的字符后剩余的新字符串。
String str = s.substring(s.lastIndexOf("i"));//9
System.out.println("str:"+str);//idg

//substring(int x , int y);该方法是获得从原字符串截取索引从x到y(不含y)的字符(包含x不包含y的)组成的字符串片段。
String str2 = s.substring(3,5);
 String[] s2 = s.split("S");
System.out.println(str2.hashCode()+">>>>>>>>>>>>"+str.hashCode());//2656>>>>>>>>>>>>>104108
System.out.println(str2==str);//false
System.out.println("str2:"+str2); //SS
 
/***
* public static void main(String[] args) {
*   Students s1=new Students();
*   Students s2=new Students();
*  Students a=s1;    //a 是一个引用变量 只有new 才会在堆中创建一个对象
*  Students a=s2;
*  
*  
*  截取获得新字符串和字符串转化成数组,这两种方式都是获得了新的String对象,即两个不同的String对象。(地址值改变)
     *  像a=new string("hello");创建了a对象好像就不能对这个字符串对象操作了,变成"hell"就是a指向另外一个对象了
*  }

*/
}
}




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值