Java: String类(构造方法)

本文详细讲解了Java中的String对象构造、字符串并置、equals与compareTo方法,以及indexOf、lastIndexOf、substring和trim等实用功能。通过实例演示了字符串操作的原理和应用场景。
摘要由CSDN通过智能技术生成

1. 构造对象:

String对象创建的对象具有两个基本值:引用、实体;
  1、String常量放在常量池中,常量池中的数据在程序运行期间不允许再次改变;
  2、String对象变量中存放着引用,new后首先分配内存空间并在内存空间中放入字符序列,然后计算出引用; 
  3、new出的String对象放在动态区中,而非常量池中,尽管在实体上一样,但引用不同;

2. 字符串并置(String + String):

public class Example8_1 {
	public static void main(String[] args) {
		String a0 = "java";
		String a1 = "ja" + "va";
        String a11 = new String("java");
		System.out.println(a0 == a1);              // true
		System.out.println(a0 == a11);             // false


		System.out.println("java" == a0);          // true


		String a = "ja";
		String b = "va";
		String c = a + b;                          // 并置运算
		System.out.println(a0 == c);               // false
		String a2 = a + b;
		System.out.println(a2 == c);               // false
	}
}

3. public boolean equals(String s) || startsWith(String s,int toffset) || startsWith(String s) || endsWith(String s):

  1. equals(String s)方法比较的是当前String对象的字符序列;
  2. equalsIgnoreCase(String s)方法忽略大小写的字符序列进行比较;
  3. startsWith(String s),判断当前String对象的字符序列前缀是否是参数指定的String对象s的字符序列;
  4. startsWith(String s,int toffset),从第toffset位的位置开始查找;
  5. endsWith(String s),判断当前String对象的字符序列后缀是否是参数指定的String对象s的字符序列.
public class Example8_2 {
	public static void main(String[] args) {
		String a0 = "java";
		String a1 = a0 + "";
        System.out.println(a1.equals(a0));                   // false
		

		String a3 = new String("java1");
		String a4 = "Java1";
		System.out.println(a3.equalsIgnoreCase(a4));         // true


        String a11 = "坚持学习java基础";
		String a12 = "好好学习,天天向上";
		System.out.println(a11.startsWith("坚持"));           // true
		System.out.println(a12.startsWith("不好好"));         // false


		System.out.println(a11.startsWith("学", 2));         // true 0->坚
		System.out.println(a12.startsWith("向上", 8));       // false 8->上

		
		System.out.println(a11.endsWith("坚持学习"));        // false
		System.out.println(a12.endsWith("天天向上"));        // true
	}
}

4. public int compareTo(String s):

        String对象调用compareTo(String s)方法时,按字典序与参数指定的String对象s的字符串序列进行比较大小,

        若相同,则返回值为0

        若大于s的字符序列,则返回正数数值;

        若小于s的字符序列,则返回负数数值。

public class Example8_4 {
	public static void main(String[] args) {
		String[] a = {"java", "c", "c#", "c++", "python"};
		String[] b = {"String", "Integer", "Long", "Double", "Date"};

		System.out.println("使用SortString类方法按字典序进行排列:");
		SortString.sortDictionary(a);
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}

		System.out.println("\n使用Arrays类方法按字典序进行排列:");
		Arrays.sort(b);
		for (int i = 0; i < b.length; i++) {
			System.out.print(b[i] + " ");
		}

	}
}

class SortString {
	public static void sortDictionary(String[] args) {
		String middle = null;
		//  最大循环次数
		for (int i = 0; i <= args.length; i++) {
			//  单个数值对所有数值的最大比较次数
			for (int j = i + 1; j < args.length; j++) {
				//  若args[j] > args[i],则返回正值大于0,相反小于0
				if (args[j].compareTo(args[i]) < 0) {
					//  进行数值替换,实现最小的放在第一位
					middle = args[i];
					args[i] = args[j];
					args[j] = middle;
				}
			}
		}
	}
}

5. public boolean contains(CharSequece s):

        判断当前String对象s的字符序列是否包含CharSequece参数s的字符序列
        System.out.println("索引位置都从0开始!!");
		String s = "student";
		System.out.println(s.contains("stt"));               // false

6. public int indexOf(String strFirst)、public int lastIndexOf(String s):

        String对象从当前String对象的字符序列的0索引位置开始检索首次出现strFirst的字符序列的位置,并返回该位置,若没有检索到,则返回 -1 ;

        String strFirst = "from zero,get string and return it\'s string location";
		System.out.println(strFirst.indexOf("string"));              // 14
		System.out.println(strFirst.indexOf("string123"));           // -1
		System.out.println(strFirst.indexOf("string", 20));          // 37

        String对象从当前String对象的字符序列的0索引位置开始检索最后一次出现strFirst的字符序列的位置,并返回该位置,若没有检索到,则返回 -1 .

		String strLast = "from last,get string and return it\'s string location";
		System.out.println(strLast.lastIndexOf("string"));            // 37
		System.out.println(strLast.lastIndexOf("string", 15));        // 14

7. public String subString(int startpoint) ---- (int start,int end):

        获取一个String对象的字符序列中从startpoint位置至最后位置上的字符所得到的的字符序列 ---- 从start到end - 1的位置;

		String strSub = " + Operation - ";
		System.out.println(strSub.substring(3));                //Operation - 
		System.out.println(strSub.substring(3, 12));            //Operation

8. public String trim():

        获取新的当前String对象的字符序列去掉前后空格后的字符序列的String对象;

		String strTrim= "  开头两个空格和末尾一个空格 ";
		System.out.println(strTrim.substring(0).trim()); //开头两个空格和末尾一个空格
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值