java实用教程——常用实用类——String类(字符串类)

JAVA把String类定义为final类(因此用户不能扩展String类,即String类不可以有子类)
String对象可以用"+"进行并置运算
identityHashCode会返回对象的hashCode,而不管对象是否重写了hashCode方法。

public class Example8_1 {
	  public static void main(String args[]) {
	       String hello = "你好";
	       String testOne = "你"+"好";             //【代码1】
	       int address =System.identityHashCode("你好");
	       System.out.printf("\"你好\"的引用:%x\n",address);
	       address =System.identityHashCode(hello);
	       System.out.printf("hello的引用:%x\n",address);
	       address =System.identityHashCode(testOne);
	       System.out.printf("testOne的引用:%x\n",address);
	       System.out.println(hello == testOne);   //输出结果是true
	       System.out.println("你好" == testOne);  //输出结果是true
	       System.out.println("你好" == hello);    //输出结果是true
	       String you = "你";
	       String hi = "好";
	       String testTwo = you+hi;                //【代码2】
	       address =System.identityHashCode("你");
	       System.out.printf("\"你\"的引用:%x\n",address);
	       address =System.identityHashCode("好");
	       System.out.printf("\"好\"的引用:%x\n",address);
	       address =System.identityHashCode(testTwo);
	       System.out.printf("testTwo的引用:%x\n",address);
	       System.out.println(hello == testTwo);   //输出结果是false
	    }
	}

在这里插入图片描述
String类中的方法:
1.public int length():用来获取一个String对象的字符序列

int number=s1.length();

2.public boolean equals(String s)用于比较当前String 对象的字符序列是否与s指定的String对象的字符序列相同

System.out.println(s1.equals(s2));

3.public boolean startsWith(String s)
判断当前String对象的字符串序列的前缀是否是参数指定的String对象s的字符序列
public boolean endsWith(String s)

4.public int compareTo(String s)
按照字典序与参数指定的String对象S的字符串序列比较大小

单词的排序
用Array类自带的方法 Arrays.sort(b);
b里面有所有的单词组成的大字符串

package lg;

import java.util.*;
public class Example8_3 {
    public static void main(String args[]) {
       String [] a={"melon","apple","pear","banana"};
       String [] b={"西瓜","苹果","梨","香蕉"};
       System.out.println("使用SortString类的方法按字典序排列数组a:");
       for(int i=0;i<a.length;i++) {
           System.out.print(" "+a[i]);
        }
       System.out.println(" ");
       SortString.sort(a);
       for(int i=0;i<a.length;i++) {
          System.out.print("  "+a[i]);
       }
       System.out.println("");
       System.out.println("使用类库中的Arrays类,按字典序排列数组b:"); 
       Arrays.sort(b);
       for(int i=0;i<b.length;i++) {
          System.out.print("  "+b[i]);
       }
    }
}


public class SortString {
   public static void sort(String a[]) {
      int count=0; 
      for(int i=0;i<a.length-1;i++) {
          int saveMinIndex = i;
          for(int j=i+1;j<a.length;j++) { 
              if(a[j].compareTo(a[saveMinIndex])<0) {
                  saveMinIndex = j;
              }
          } 
          String temp=a[i];
          a[i]=a[saveMinIndex];
          a[saveMinIndex]=temp;
      }
   }
}

在这里插入图片描述
5.public boolean contains(String s)
判断当前String的对象字符序列是否包含参数S的字符序列
6.public int indexOf(String s)
从当前String对象的字符序列的0索引位置开始检索首次出现str的字符序列的位置,并返回位置
public int lastIndexOf(String s)
从当前String对象的字符序列的0索引位置开始检索最后一次出现str的字符序列的位置,并返回位置

找不到返回-1

String tom = "I am a good cat";
tom.indexOf("a");//值是2
tom.lastIndexOf("a");//值是13

7.public String substring(int startpoint)
substring(int startpoint)方法是字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的startpoint处截取到最后所得的字符串(注:字符串的起始位置是从0开始的,截取的时候startpoint位置的字符也被截取)

substring(int start,int end)
方法是获取一个当前字符串的子串,该子串是通过复制当前字符串start到end-1位置上的字符串(注:字符串的起始位置是从0开始)
8。public char charAt(int index)
charAt()方法是用来输出一个字符串中的单个字符,例如:
String s = “hello world”;
system.out.println(s.charAt(1));
输出的结果就为e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值