J6 String字符串

1.String字符串

1.1实例化String对象

  • 直接赋值(推荐使用)
  • 使用关键字new,因为String本身就是个对象
  • 示例
package com.hanqi.string;

public class test01 {

	public static void main(String[] args) {
		
//		String str = "haha";	// 直接赋值
		String str = new String("haha");	//new赋值
		System.out.println(str);
		
	}

}

1.2String注意事项

  • String内容不可被更改,下述举止非法
package com.hanqi.string;

public class test03 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String a = "haha";
		String a = "hehe";
	}
}

2.String字符串常用方法

2.1字符串比较

  • 使用equals方法比较
  • 例如
package com.hanqi.string;

public class test02 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String a = "haha";
		String b = new String("haha");
		System.out.println(a.equals(b));	//equals比较的是内容
		System.out.println(a == b);		    //==比较的是地址
	}
}
///
true
false

2.2字符串长度

  • length方法计算字符串长度
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = "hanqi";
		System.out.println(str.length());
	}
}
//
5

2.3字符串转数组

  • toCharArray方法
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = "hanqi";
		char data[] = str.toCharArray();
		for(int i = 0; i < data.length; i++)
		{
			System.out.print(data[i]);
		}
	}
}
//
hanqi

2.4从字符串中取出指定位置的字符

  • charAt方法,从下标0开始
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = "hanqi";
		System.out.println(str.charAt(3));
	}
}
//
q

2.5字符串与byte数组的转换

  • getBytes方法
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = "hanqi";
		byte bytes[] = str.getBytes();
		for(int i = 0; i < bytes.length; i++)
		{
			System.out.println(new String(bytes));
		}
	}
}
//
hanqi
hanqi
hanqi
hanqi
hanqi

2.6查找字符串中存在的字符位置

  • indexOf方法,存在指定字符串则返回指定字符串首次出现的位置,没有则返回-1
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = "hanqi";
		System.out.println(str.indexOf("d"));
	}
}
//
-1
0

2.7去掉字符串的前后空格

  • trim方法
  • 示例
package com.hanqi.string;

public class test04 {

	public static void main(String[] args) {
		String str = " ha nq i ";
		System.out.println(str);
		System.out.println(str.trim());
	}
}
//
 ha nq i 
ha nq i

2.8其它

  • subString:从字符串中取出子字符串
  • toLowerCase,toUpperCase:大小写转换
  • endsWith,startWith:判断字符串的开头结尾字符
  • replace:替换String字符串中的一个字符

3.StringBuffer

3.1定义

  • 即缓冲区,本身也是操作字符,但与String不同,StringBuffer可更改
  • StringBuffer是一个操作类,必须通过实例化进行操作

3.2举例

  • 示例
package com.hanqi.string;

public class test05 {

	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		sb.append("hanqi");
		System.out.println(sb.toString());
		tell(sb);
		System.out.println(sb.toString());
	}
	public static void tell(StringBuffer s) {
		s.append("nb");
	}
}
//
hanqi
hanqinb

2.3StringBuffer常用方法

  • append:追加字符串
  • insert:在某个位置插入字符串
  • replace:将从下标a开始至下标b的内容替换为另一个字符串
  • indexOf查找
  • 示例
package com.hanqi.string;

public class test05 {

	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		sb.append("hanqi");
		System.out.println(sb.toString());
		sb.insert(0, "nb");
		System.out.println(sb.toString());
		sb.replace(0, 2, "mn");
		System.out.println(sb.toString());
		System.out.println(sb.indexOf("han"));
	}
}
//
hanqi
nbhanqi
mnhanqi
2
  • 当buff的值需要频繁改变时使用StringBuffer效率更高

4.StringBuilder

4.1概念

  • 一个可变的字符序列,该类被设计作用DtringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候.建议优先考虑该类,其速度比StringBuffer更快
  • 但是如果涉及到线程安全方面,建议使用StringBuffer
posted @ 2021-08-05 23:32  MHDSG  阅读(39)  评论(0)    收藏  举报