12-6java面向对象之String类常用方法的总结

<span style="font-family: Arial, Helvetica, sans-serif;">我们都知道,String类是在java程序开发过程中使用比较多的类,必须将其完全掌握并熟练运用才行。基于这个原因,作者打算把String类中常用的方法进行总结 ,方便大家学习。</span>

1.字符串与字符数组

序号 类型 名称 说明
1 构造 public String(char[] value) 把字符数组转化为字符串
2 构造 public Stringchar[] value, int offset, int count 从offset开始,将count个数组转化为字符串
3 方法 public char charAt(int index) 返回制定位置的字符
4 方法 public char[] toCharArray() 把字符串转化为数组

案例:

public class StringTest
{
	public static void main(String[] args)
	{
		char ch[]={'a','b','c'};	//定义原始的字符数组
		char ch2[]= null;			//字符串转化之后保存数组
		String str1=new String(ch);		//全部转化
		String str2=new String(ch,1,2);	//从1开始,转化2个
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str1.charAt(1));	//str1的搜索指定位置的字符
		ch2= str1.toCharArray();			//str1字符串转化为字符数组
		for (int i=0;i<ch2.length ;++i )
		{
			System.out.println(ch2[i]);
		}
	}
}
结果:

2.字符串与字节数组

序号 类型 名称 说明
1 构造 public Stringbyte[] value 把字节数组转化为字符串
2 构造 public Stringbyte[] value, int offset, int count 从offset开始,将count个数组转化为字符串
3 方法 public byte[] getBytes() 把字符串转化为字符数组

案例:
public class StringTest
{
	public static void main(String[] args)
	{
		String str="hello world";<span style="white-space:pre">	</span>//定义字符串
		byte b[]= str.getBytes();<span style="white-space:pre">	</span>//字符串转换为字节
		System.out.println(new String(b));
	}
}

结果

注意:在IO操作上经常会使用字符串和字节之间的转换。

3.字符串的比较

序号 类型 名称 说明
1 方法 public boolean equals(String str) 比较两个字符串是否相等,与大小写有关
2 方法 public boolean equalsIgnoreCase(String str) 比较两个字符串是否相等,与大小写无关
3 方法 public int compareTo(String str) 返回值得到相等(0),大于(>0),小于(<0)
程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String str1="hello world!";
		String str2="HELLO WORLD!";
		System.out.println("相等判断(区分大小写)");
		System.out.println(str1.equals(str2));
		System.out.println("相等判断(不区分大小写)");
		System.out.println(str1.equalsIgnoreCase(str2));
		System.out.println("比较大小");
		System.out.println(str1.compareTo(str2));
	}
}
结果

4.查找字符串

序号 类型 名称 说明
1 方法 public boolean contains(String str) 判断是否存在字符串str
2 方法 public int indexOf(String str) 返回查找到字符串的位置
3 方法 public int indexOf(String str, int indexfrom) 从indexfrom之后开始查找
4 方法 public int lastIndexOf(String str) 从后向前找
5 方法 public int lastIndexOf(String str int indexfrom) 从后向前找,从指定位置

程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String str1="hello Tyrion!";
		String str2="el";
		System.out.println("搜索是否包含:");
		System.out.println(str1.contains(str2));
		System.out.println(str1.indexOf(str2));
		System.out.println(str1.indexOf(str2,4));
		System.out.println(str1.lastIndexOf(str2));
		System.out.println(str1.lastIndexOf(str2,2));
	}
}
结果

5.判断开头结尾

序号 类型 名称 说明
1 方法 public boolean startWith(String str) 判断是否以字符串str开头
2 方法 public boolean endWith(String str) 判断是否以字符串str结尾
程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String den="hello  world";
		String sta="he";
		String end="ld";
		if (den.startsWith(sta))
		{
			System.out.println("是指定的开头");
		}
		if (den.endsWith(end))
		{
			System.out.println("是指定的结尾");
		}
	}
}
结果

6.字符串截取

序号 类型 名称 说明
1 方法 public String substring(int index) 从index位置开始截取剩余字符串
2 方法 public String substring(int i, int j) 从i 到j 之间的字符串进行截取
程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String den="hello  world";
		String str=null;
		str=den.substring(6);
		System.out.println(str);
		str=den.substring(2,6);
		System.out.println(str);

	}
}
结果

7.字符串替换

序号 类型 名称 说明
1 方法 public String replaceAll(String exp, String replace) 把exp全部用replace替换
2 方法 public String replaceFirst(String exp, String replace) 把第一个exp用replace替换

程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String den="hello  world hello";
		String str="hello";
		System.out.println(den.replaceAll(str,"nihao"));
		System.out.println(den.replaceFirst(str,"nihao"));

	}
}
结果

7.字符串拆分

序号 类型 名称 说明
1 方法 public String[] split(String exp) 把exp进行拆分,得到String数组
2 方法 public String[] split(String exp, int limit) 把exp进行拆分制定limit个,得到String数组
程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String str="hello world";
		String out[]=str.split("o");
		for (int i=0;i<out.length ;++i )
		{
			System.out.println(out[i]);
		}
		String out2[]=str.split("o",2);
		for (int i=0;i<out2.length ;++i )
		{
			System.out.println(out2[i]);
		}
	
	}
}
结果

注意:拆分之后的数组个数满足要求个数之后,剩余的不要拆分。如果有IP地址,以'.'作为拆分表达式,此时拆不开。‘.’是正则规则的符号,要拆分必须使用转义符\\.   拆不开的都用转义符。

8.其他

序号 类型 名称 说明
1 方法 public String toUpperCase() 转换成大写
2 方法 public String toLowerCase() 小写
3 方法 public String trim() 去掉左右空格
4 方法 public int length() 长度
5 方法 public boolean isEmpty() 判断字符串是否是空
程序
class StringTest 
{
	public static void main(String[] args) 
	{
		String str=" hello WORLD ";
		System.out.println("转大写");
		System.out.println(str.toUpperCase());
		System.out.println("转小写");
		System.out.println((str.toUpperCase()).toLowerCase());
		System.out.println("去除空格");
		System.out.println(str.trim());
		System.out.println("是否为空:");
		System.out.println(str.isEmpty());
	}
}
结果
字符串的首字母大写
class StringTest 
{
	public static void main(String[] args) 
	{
		String s="hello";
		System.out.println(initcap(s));
	}
	public static String initcap(String str)
	{
		return str.substring(0,1).toUpperCase()+ str.substring(1);
	}
}

结果

经典题目:输入如下字符串:张三:11|李四:22|王五:33
使用String类进行分类,显示如图:
class StringTest 
{
	public static void main(String[] args) 
	{
		String str="张三111|李四122|王五133";
		int mid =0;		//:的标志
		int end =0;		//:成绩结束的标志
		int start =0;	//姓名开始的标志
		mid = str.indexOf(":");
		end =str.indexOf("|",mid);
		start = str.lastIndexOf("|",mid);
		if(mid ==-1)
		{	
			System.out.println("数据格式不对!");
		}
		while (mid !=-1)		//存在:则向前找|作为开始的标志
		{
			if (start ==-1)		//说明|存在,这个名字是第一个姓名
			{
				System.out.println( "姓名:" + str.substring(0,mid) + ",   " + "成绩" + 
					str.substring(mid,end) + "\n");
			}
			else if(end ==-1)			//start不是-1说明姓名不是第一个,但是此时还要判断是否结束-1则结束
			{
				System.out.println( "姓名:" + str.substring(start,mid) + ",   " + "成绩" + 
					str.substring(mid,str.length()) + "\n");
			}
			else	//说明是中间的
			{	
				System.out.println( "姓名:" + str.substring(start,mid) + ",   " + "成绩" + 
					str.substring(mid,end) + "\n");
			}
			mid = str.indexOf(":",mid+1);//继续向后寻找,直至-1
			end =str.indexOf("|",mid);
			start = str.lastIndexOf("|",mid);
		}
	}
}
方法2:使用分割的方式
class StringTest 
{
	public static void main(String[] args) 
	{
		String str ="张三:11|李四:22|王五:33";
		String s[] = str.split("\\|");
		for (int i =0;i<s.length ; ++i)
		{
			String temp[] =s[i].split(":");
			System.out.println("姓名:" + temp[0] + ",  " + "成绩:"+ temp[1]);				
		}
	}
}


总结:一定要掌握所有的这些方法。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值