`
未来程序员
  • 浏览: 25152 次
社区版块
存档分类
最新评论

JAVA的String 类

 
阅读更多

String就是C++、java等编程语言中的字符串,用双引号引起来的几个字符.如"Abc","一天".

 

 

特别注意

String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象.这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似
.String的几个用法
1.将字符串中的小写字母转换成大写字母
	private void UpperCase() {
		String string = "abc";
		
		System.out.println(string.toUpperCase());//ABC
	}
	
 2.比较
	private static void Compare()
	{
		String str1 = "abc";
		String str2 = "abc";
		
		System.out.println(str1.compareTo(str2));//相等为 0 str1比str2大为1 小为-1
		
	}
 3.除去字符串前和后的空格(字符串中间的不去除)
	private static void  Trim() {
		
		String str1 = "     abc   123                  ";
		
		System.out.println(str1.trim());
              //结果   abc   123
		
	}
 4.返回指定位置的字符(从0开始)
	private static void Location() {//返回指定位置的字符
		
		String str1 = "abcde";
		System.out.println(str1.charAt(2));
		//    c
	}
 5.将两个字符串连接
	private static void Concat() {//连接
		
		String str1 = "abc     ";
		String str2 = "def";
		System.out.println(str1.concat(str2));
		
	}
 6.判断是否为指定字符结尾 不是返回false
	private static void Endwith() {//是否为指定字符结尾 不是返回false
		
		String str1 = "abcdef";
		System.out.println(str1.endsWith("de"));
		//false
	
	}
	
 7.判断是否为指定字符开头 不是返回false
private static void Startswith() {
		
		String str1 = "abcdefg";
		System.out.println(str1.startsWith("ab"));//true
		System.out.println(str1.startsWith("v"));//false
		
		
	}
 8.返回某个字符所在字符串出现的第一个位置
private static void  IndexOf() {//返回某个字符的第一个位置
		
		String str1 = "aabbccdd";
		System.out.println(str1.indexOf("b"));//2

	}
 9.替换
	private static void Replace() {
		
		String str1 = "abababcdcdcd";
		String str2 = str1.replace("ab", "12");
		System.out.println(str1);//abababcdcdcd
		System.out.println(str2);//121212cdcdcd
		
	}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics