一、字符串与字符
(1)String(char[] value)
分配一个新String字符,使其代表当前包含在character array参数中的字符序列。
(2)String(char[] value, int offset, int count)
分配一个新String字符,其中包含来自character array参数的子数组的字符。
(3)public char charAt(int index)
返回char指定索引处的值。

public static void main(String[] args) {
		String msg="hello WoRld";
		char c=msg.charAt(2);
		System.out.print(c);
	}

输出:l

(4)String的toCharArray()方法:把字符串转化成字符数组
举例:

public static void main(String[] args) {
		String msg="1235a67";
		System.out.print(isNumber(msg));
	}
	public static boolean isNumber(String str) {
		char []data=str.toCharArray();//把字符串转化成字符数组,String转char数组
		for(int i=0;i<data.length;i++)
		{
			if(data[i]>'9'||data[i]<'0')
				return false;
		}return true;
	}

二、字符串与字节
(1)String(byte[] bytes)
String通过使用平台的默认字符集对指定的字节数组进行解码,构造一个new 。
(2)String(byte[] bytes, int offset, int length)
String通过使用平台的默认字符集对指定的字节子数组进行解码来构造一个新数组。
(3)public byte[] getBytes()
String使用平台的默认字符集将其编码为字节序列,并将结果存储到新的字节数组中。
(4)public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException
String使用命名的字符集将此编码为字节序列,并将结果存储到新的字节数组中。编码转换

public static void main(String[] args) {
		String msg="hello world";
		byte[] data=msg.getBytes();
		for(int i=0;i<data.length;i++)
		{
			System.out.println(data[i]+" ");
			data[i]-=32;
		}
		System.out.println(new String(data));
	}
System.out.print(new String(data));

输出:
104
101
108
108
111
32
119
111
114
108
100
HELLO

new String()可以把数组转化成字符串
字符串与字节数组的转换用途:IO编程和网络编程
三、字符串的比较
(1)public boolean equals(Object anObject)
将此字符串与指定对象进行比较。
(2)public boolean equalsIgnoreCase(String anotherString)
将其String与另一个比较String,忽略大小写考虑。

public static void main(String[] args) {
		String msgA="helloworld";
		String msgB="HELLOWORLD";
		System.out.println(msgA.equals(msgB));
		System.out.println(msgA.equalsIgnoreCase(msgB));
	}

输出:
false
true

(3)public int compareTo(String anotherString)
按字典顺序比较两个字符串。

public static void main(String[] args) {
		String msgA="helloworld";
		String msgB="HELLOWORLD";
		System.out.println(msgA.compareTo(msgB));
	}

输出结果:32

四、字符串查找
(1)public boolean contains(CharSequence s)
当且仅当此字符串包含指定的char值序列时,才返回true。

public static void main(String[] args) {
		String msg="helloworld";
		System.out.println(msg.contains("hello"));
	}

输出:true

(2)public int indexOf(String str)
返回指定子字符串首次出现在该字符串中的索引。

public static void main(String[] args) {
		String msg="helloworld";
		System.out.println(msg.indexOf("w"));
		System.out.println(msg.indexOf("hello"));
		System.out.println(msg.indexOf("nihao "));
	}

输出:
5
0
-1

(3)public int indexOf(String str, int fromIndex)
从指定的索引开始,返回指定子字符串首次出现在该字符串中的索引。

(4)public int lastIndexOf(String str)
返回最后一次出现的指定子字符串在此字符串内的索引
(5)public int lastIndexOf(String str,int fromIndex)
返回最后一次出现的指定子字符串在此字符串内的索引,从指定索引开始向后搜索。
(6)public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开头。
(7)public boolean startsWith(String prefix, int toffset)
测试此字符串的子字符串是否从指定的索引开始,以指定的前缀开头。
(8)public boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结尾。
(7)(8)在购物车模型中会用到,因为要处理动态生成的表单数据
五、字符串替换
(1)String replaceAll(String regex, String replacement)
用给定的替换项替换该字符串中与给定的正则表达式匹配的每个子字符串。
(2)String replaceFirst(String regex, String replacement)
用给定的替换项替换与给定的正则表达式匹配的此字符串的第一个子字符串。

public static void main(String[] args) {
		String msg="helloworld";
		System.out.println(msg.replaceAll("l","_"));
		System.out.println(msg.replaceFirst("l","_"));
	}

输出:
he__owor_d
he_loworld

六、字符串拆分
字符串拆分后形成字符串数组
(1)String[] split(String regex)
在给定正则表达式的匹配项周围拆分此字符串。
(2)String[] split(String regex, int limit)
在给定正则表达式的匹配项周围拆分此字符串 。

public static void main(String[] args) {
		String msg="hello hello hello world";
		String[] s=msg.split(" ",2);
		for(int i=0;i<s.length;i++)
		{
			System.out.println(s[i]);
		}
	}

输出:
hello
hello hello world

public static void main(String[] args) {
		String msg="192.168.10.12";
		String[] s=msg.split("\\.");
		for(int i=0;i<s.length;i++)
		{
			System.out.println(s[i]);
		}
	}

输出:
192
168
10
12
注:在编写程序时,可能会遇到无法拆分的时候,比如说“.”,这是因为正则表达式的原因,因此要加上\,才可以完成拆分。“_”不需要加\。
七、字符串截取
(1)String substring(int beginIndex)
返回一个字符串,该字符串是该字符串的子字符串。
(2)String substring(int beginIndex, int endIndex)
返回一个字符串,该字符串是该字符串的子字符串。

public static void main(String[] args) {
		String msg="hello world";
		System.out.println(msg.substring(6));
		System.out.println(msg.substring(6,10));
	}

输出:
world
worl
八、其它方法
(1)String concat(String str)
将指定的字符串连接到该字符串的末尾。
(2)String intern()
返回字符串对象的规范表示。入池
(3)boolean isEmpty()
返回true,当且仅当,length()是0。
(4)int length()
返回此字符串的长度。
(5)String toLowerCase()
String使用默认语言环境的规则将此字符转换为小写。
(6)String toUpperCase()
String使用默认语言环境的规则将此字符转换为大写。
(7)String trim()
返回值是此字符串的字符串,其中已删除所有前导和尾随空格。

public static void main(String[] args) {
		String msg="     hello world     ";
		System.out.println(msg.isEmpty()?"kongkong":"字符串不为空");
		System.out.println(msg.length());
		System.out.println("原始字符串【"+msg+"】处理后字符串【"+msg.trim()+"】处理后字符串长度【"+msg.trim().length()+"】");
		System.out.println("转小写:"+msg.toLowerCase());
		System.out.println("转大写:"+msg.toUpperCase());
	}

输出:
字符串不为空
21
原始字符串【 hello world 】处理后字符串【hello world】处理后字符串长度【11】
转小写: hello world
转大写: HELLO WORLD

public static void main(String[] args) {
		String msg="hello WoRld";
		System.out.println(initCap(msg));
	}
	public static String initCap(String str)
	{
		return str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
	}

输出:Hello world