【Java】字符串(一)

 

目录

一、创建字符串

二、连接字符串

连接多个字符串

连接其他数据类型

三、获取字符串的信息

获取字符串的长度

字符串查找

获取指定索引位置的字符

四、字符串操作

获取子字符串

去除空格

字符串的替换

判断字符串是否相等

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

字母大小写转换

字符串分割


一、创建字符串

(1)String(char a[])

用一个字符数组a创建一个String对象

char a[] = {'a','b','c','d'};
String s = new String(a);

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

提取a数组中的一部分,创建一个字符串对象,offset表示开始截取字符的位置

char a[] = {'a','b','c','d'};
String s = new String(a,2,2);

(3)String(char[] value)

String s = new String("Student");

(4)引用字符串常量来创建字符串变量

String s1;
s1 = "student";

二、连接字符串

连接多个字符串

使用“+”可实现多个字符串连接的功能

public class practice {

	public static void main(String[] args) {
		String s1 = new String("Hello");
		String s2 = new String("World");
		String s = s1 + " " + s2;
		System.out.println(s);
	}

}

注意:Java中一句相连的字符串不能分开在两行写。如果字符串太长,可以将字符串写在两行但是需要使用“+”连接。

连接其他数据类型

字符串也可以同其他数据类型进行连接。连接时,会将这些数据直接转换成字符串。

public class practice {

	public static void main(String[] args) {
		int a = 4;
		float b = 4.5f;
		System.out.println("sum" + a + b); // sum44.5
	}

}

注意区别,因为圆括号的优先级高

public class practice {
	
	public static void main(String[] args) {
		int a = 4;
		float b = 4.5f;
		System.out.println("sum" + (a+b)); // sum8.5
	}
	
}

三、获取字符串的信息

获取字符串的长度

String str = "1234567890"; 
int size = str.length();
System.out.println("str.length() = " + size); // str.length() = 10

字符串查找

String类提供两种查找查找字符串的方法。

indexOf() 与 lastIndexOf() :都允许在字符串中搜索指定条件的字符或者字符串。

indexOf()方法返回的是搜索的字符或者字符串首次出现的位置,lastIndexOf()方法返回的是最后一次出现的位置。

String str = "00112233445566778899"; 
int pos1 = str.indexOf("0");
int pos2 = str.lastIndexOf("0");
System.out.println("pos1 = " + pos1); // pos1 = 0
System.out.println("pos2 = " + pos2); // pos2 = 1

 

public class practice {
	
	public static void main(String[] args) {
		String str = "we are students";
		int pos = str.lastIndexOf("");
		System.out.println("空字符在字符串str中的索引位置是" + pos); 
		//空字符在字符串str中的索引位置是15
		System.out.println("字符串str的长度是" + str.length());
		//字符串str的长度是15
	}
	
}

获取指定索引位置的字符

使用charAt()方法可以可以将指定索引处的字符返回。

public class practice {
	
	public static void main(String[] args) {
		String str = "hello world";
		char mychar = str.charAt(6);
		System.out.println(mychar); // w
	}
	
}

四、字符串操作

获取子字符串

substring()方法可以对字符串进行截取。利用下标进行截取,且应明确字符串下标应从0开始。

(1)substring(int beginIndex),其中beginIndex指定从某一索引处开始截取字符串。

String str = "hello world";
String s = str.substring(3);
System.out.println(s); //lo world

(2)substring(int beginIndex,int endIndex)

beginIndex , endIndex )

String str = "hello world";
String s = str.substring(3,7);
System.out.println(s); //lo w

去除空格

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

String str = "     0123 5678    ";
System.out.println("原始长度" + str.length()); // 原始长度18
String s = str.trim();
System.out.println(s); //0123 5678
System.out.println("去掉前导空格和尾部空格之后的长度是" + s.length()); 
//去掉前导空格和尾部空格之后的长度是9

字符串的替换

replace()方法可实现将指定字符或者字符串替换成新的字符或者字符串

str.replace(char oldChar,char newChar)

如果oldChar没有出现在该对象表达式中的字符串序列中,则将原字符串返回。

是将所有的字符oldChar转换成newChar

public class practice {
	
	public static void main(String[] args) {
		String str = "address_a";
		String newstr = str.replace("a", "A");
		System.out.println(newstr); //Address_A
	}
	
}

判断字符串的开始与结尾

startsWith() 和 endsWith() 方法分别用于判断字符串是否以指定的内容开始或结束。这两个方法的返回值都是boolean类型。

(1)startWith()方法

该方法用于判断当前字符串对象的前缀是否为参数指定的字符串。

str.startsWith(String prefix),其中指作为前缀的字符。

(2)endsWith()方法

该方法用于判断当前字符是否为以给定的子字符串结束。

str.endsWith(String suffix),其中suffix是指作为后缀的字符串。

public class practice {
	
	public static void main(String[] args) {
		String num1 = "22045612";
		String num2 = "21304578";
		boolean b = num1.startsWith("22");
		boolean b2 = num1.endsWith("78");
		boolean b3 = num2.startsWith("22");
		boolean b4 = num2.endsWith("78");
		System.out.println("字符串num1是以‘22’开始的吗?"+b); // 字符串num1是以‘22’开始的吗?true
		System.out.println("字符串num1是以‘78’结束的吗?"+b2); // 字符串num1是以‘78’结束的吗?false
		System.out.println("字符串num2是以‘22’开始的吗?"+b3); // 字符串num2是以‘22’开始的吗?false
		System.out.println("字符串num2是以‘78’结束的吗?"+b4); // 字符串num2是以‘78’结束的吗?true
	}
	
}

判断字符串是否相等

比较运算符“==”,因为比较运算符比较的是两个字符串的地址是否相同。即使两个字符串的内容相同,两个对象的内存地址也是不同的使用比较运算符仍然会返回false。

String s1 = new String("123");
String s2 = new String("123");
boolean b = (s1 == s2);
System.out.println(b); // false

(1)如果两个字符串具有相同的字符和长度,则使用equals()方法进行比较时返回true,区分大小写

str.equals(String otherstr)

(2)在忽略大小写的情况下判断两个字符串是否相等

str.equalsIgnoreCase(String otherstr)

public class practice {
	
	public static void main(String[] args) {
		String s1 = new String("abc");
		String s2 = new String("ABC");
		String s3 = new String("abc");
		boolean b = s1.equals(s2);
		boolean b2 = s1.equalsIgnoreCase(s2);
		System.out.println(s1 + " equals " + s2 + ":" + b); 
		// abc equals ABC:false
		System.out.println(s1 + " equalsIgnoreCase " + s2 + ":" + b2); 
		// abc equalsIgnoreCase ABC:true
	}
	
}

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

str.compareTo(String otherstr)

如果按照字典顺序将此String对象位于参数字符串之前则比较结果为一个负整数;

如果按照字典顺序将此String对象位于参数字符串之后则比较结果为一个正整数;

如果两个字符串相等,则结果为0;

public class practice {
	
	public static void main(String[] args) {
		String s1 = new String("b");
		String s2 = new String("a");
		String s3 = new String("e");
		System.out.println(s1 + " compare to " + s2 + ":" + s1.compareTo(s2));
		//b compare to a:1
		System.out.println(s1 + " compare to " + s3 + ":" + s1.compareTo(s3));
		//b compare to e:-3
	}
	
}

字母大小写转换

str.toLowerCase() 将String转换成小写

str.toUpperCase() 将String转换成大写

public class practice {
	
	public static void main(String[] args) {
		String str = "abc DFE";
		String newstr1 = str.toLowerCase();
		String newstr2 = str.toUpperCase();
		System.out.println(newstr1);//abc dfe
		System.out.println(newstr2);//ABC DFE
	}
	
}

字符串分割

(1)split(String sign)

str.split(String str)

该方式是根据给定的分割符对字符串进行拆分。

(2)split(String sign,int limit)

str.split(String sign,int limit)

该方法根据给定的分割符对字符串进行拆分,并限定拆分的次数。

public class practice {

	public static void main(String[] args) {
		String str = "192.168.0.1";
		String[] firstArray = str.split("\\.");
		String[] secondArray = str.split("\\.",2);
		System.out.print("str的原值为:[" + str + "]");
		System.out.println("全部分割结果是");
		for(String a:firstArray) {
			System.out.print("["+ a + "]");
		}
		System.out.println();
		for(String a:secondArray) {
			System.out.print("[" + a + "]");
		}
		System.out.println();
	}

}

/*
str的原值为:[192.168.0.1]全部分割结果是
[192][168][0][1]
[192][168.0.1]
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值