Java学习笔记——String类

在Java语言中字符串必须包含在一对双引号(" ")之内,换句话说,被双引号(" ")包围的都是字符串,不能作为其他数据类型来使用,如"1+2"的输出不是3。

一、字符串的创建

1、String(char a[ ])

2、String(char a[ ],int offset,int length)

用于截取字符串的某一段,offset:开始截取字符串的位置,length:要截取的长度。 

3、引用字符串常量来创建字符串变量

//1、等价于String s=new String("good");
		char a[] = {'g','o','o','d'};
		String s = new String(a);
		//2、等价于String s=new String("uden");
		char a1[] = {'s','t','u','d','e','n','t'};
		String s1=new String(a1,2,4);
		//3、等价于String s=new String("We are student");
		String str1,str2;
		str1="We are student";
		str1="We are student";

特别注意的是,用第三种方法时,str1和str2具有相同的实体(内存地址)。

二、连接字符串

“+”字符号可以连接多个运算符并产生一个String对象。

public class First {
	public static void main(String[] args) { 
		String s1 = new String("Hello");
		String s2 = new String("Java");
		String s = s1 + " " + s2;
		System.out.println(s);
	}
}
//result:Hello Java

也可以连接多种数据类型,这里可以看整理出来的Java中的加号——TP

三、获取字符串信息

(1)str.length()

返回字符串长度。

(2)str.charAt(int index)

该方法可将指定索引处(index)的字符返回。

(3)str.indexOf(String s)

该方法返回在字符串s中指定字符串首次出现的索引位置,而str.lastindexOf(String s)返回最后一次出现的位置。如果没有找到,则返回-1。

public class First {
	public static void main(String[] args) { 
		String s = new String("hello Java");
		int a = s.length();
		char b = s.charAt(2);
		int c = s.indexOf("l");
		int d = s.lastIndexOf("l");
		System.out.println(a+" "+b+" "+c+" "+d);
	}
}
//10 l 2 3

四、字符串操作

String类中包含了很多方法。

1、获取子字符串

(1)str.substring(int beginIndex)

该方法返回指定索引位置开始(beginIndex)到该字符串结尾的子串。

(2)str.substring(int beginIndex,int endIndex)

该方法返回指定索引位置开始(beginIndex/包括)到指定结束索引位置(endIndex/不包括)的子串。

 记住,字符串 位置是从0开始的。

public class First {
	public static void main(String[] args) { 
		String s = new String("abcde");
		String s1 = s.substring(2);
		String s2 = s.substring(1, 3);
		System.out.println(s1);
		System.out.println(s2);
	}
}
/*result:
cde
bc
*/

2、去除空格

str.trim( );

trim( )方法返回字符串的副本,忽略前导空格和 尾部空格, 中间的空格不考虑。

public class First {
	public static void main(String[] args) { 
		String s = new String("   ab c de   ");
		String s1 = s.trim();
		System.out.println(s1);
	}
}
//result:ab c de

3、字符串替换

(1)str.replace(char oldChar,char newChar);

如果要替换的字符oldChar在字符串中重复多次,replace()方法会将所有都替换为newChar。

(2)str.toUpperCase();

将小写字母全部替换成大写字母,其他类型字符不受影响。

(2)str.toLowerCase();

将大写字母全部替换成小写字母,其他类型字符不受影响。

public class First {
	public static void main(String[] args) { 
		String s = new String("I hate you");
		String s1 = s.replace("hate", "love");
		String s2 = s1.toUpperCase();
		String s3 = s1.toLowerCase();
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
	}
}
/*
 result:
 I love you
I LOVE YOU
i love you
 */

4、判断字符串

(1)str.startsWith(String prefix)

该方法用于判断字符串对象的前缀是否为指定(prefix)的字符串 ,返回值为boolean类型。

(2)str.endsWith(String suffix)

该方法用于判断字符串对象的后缀是否为指定(suffix)的字符串 ,返回值为boolean类型。

(3)str.equals(String otherstr)

该方法比较两个字符串内容是否相等,区分大小写,用str.equalslgnoreCase(String otherstr)方法可不区分大小写,这两种方法都是返回boolean类型。

public class First {
	public static void main(String[] args) { 
		String s = new String("abcde");
		String s1 =new String("ABCDE");
		boolean a = s1.equals(s);
		boolean b = s1.equalsIgnoreCase(s);
		boolean c = s.startsWith("b");
		boolean d = s.endsWith("de");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
	}
}
/*
false
true
false
true
 */

5、按字典顺序比较两个字符串

str.compareTo(String otherstr);

该方法按字典顺序比较两个字符串,当两个字符串相等时,结果为0,当不等时,有以下几种情况:

(1)两个字符串首字母 不同,返回首字符ascll码的差值(区分正负数)。

(2)首字母相同,则 比较下一个字符,直到有不同的为止。

(3)两个字符串不一样长,而可比较的部分一样,则返回字符串长度差值

6、字符串分割

使用sqlit()方法可以使字符串按指定的分割字符或字符串对内容进行分割,并将分割后的结果存放于分割数组中。

(1)str.split(String sign)

根据给定分隔符对字符串进行拆分。

(2)str.split(String sign,int limit)

根据给定分隔符对字符串进行拆分,并限定拆分次数。

public class First {
	public static void main(String[] args) { 
		String s = new String("192.26.14.60");
		String[] a = s.split("\\.");
		String[] b = s.split("\\.", 2);
		for(String c:a) {
			System.out.print("["+c+"]");
		}
		System.out.println();
		for(String c:b) {
			System.out.print("["+c+"]");
		}
		
	}
}
/*
[192][26][14][60]
[192][26.14.60]
 */

有关String类的基本知识就介绍到这里,之后还会干出字符串生成器等String类的东西,洗洗睡了~。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值