黑马程序员——学习日记4

String类的常用方法

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

 按照面向对象的思想对字符串进行功能分类。

  1.获取:

  1.1获取字符串中字符的个数(字符串长度)。

        int length();

 

  1.2根据位置获取字符。

        char charAt(int Index);

 

  1.3根据字符或字符串获取在字符串中第一次出现的位置。

  注意:我们可以通过-1来判断字符或者字符串是否存在。

  

  根据字符获取在字符串中第一次出现的位置。

  1.3.1.1

                从0角标顺序查找ch第一次出现的位置。

                intindexOf(int ch);

                从指定位置顺序查找ch第一次出现的位置。

                intindexOf(int ch,int fromIndex);

  1.3.1.2

                从最大角标反向查找ch第一次出现的位置。

                intlastIndexOf(int ch);

                从指定位置反向查找ch第一次出现的位置。

                intlastIndexOf(int ch,int fromIndex);

 

 根据字符串获取在字符串中第一次出现的位置。

  1.3.2.1

                从0角标顺序查找ch第一次出现的位置。

                intindexOf(String str);

                从指定位置顺序查找字符串第一次出现的位置。

                intindexOf(String str,int fromIndex);

  1.3.2.2

                从最大角标反向查找ch第一次出现的位置。

                intlastIndexOf(String str);

                从指定位置反向查找ch第一次出现的位置。

                intlastIndexOf(String str,int fromIndex);

  

  1.4获取字符串中一部分字符串。也叫子串。

  String subString(int beginIndex);//从beginIndex(包括)开始到最后。相当于s的字符串(int beginIndex,int s.length())。

  String subString(int beginIndex,int endIndex);//beginIndex包括,endIndex不包括。

示例:

<span style="font-size:18px;">public class StringMethodDemo1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s="abcdabcd";
		
		System.out.println(s.length());//8
		
		System.out.println(s.charAt(2));//c
		
		System.out.println(s.indexOf('c'));//2
		System.out.println(s.indexOf('c',3));//6
		System.out.println(s.lastIndexOf('c'));//6
		System.out.println(s.lastIndexOf('c',5));//2
		
		System.out.println(s.indexOf("bc"));//1
		System.out.println(s.indexOf("bc",2));//5
		System.out.println(s.lastIndexOf("bc"));//5
		System.out.println(s.lastIndexOf("bcd",5));//5 说明对于字符串的查找,是基于定位到的字符向右读取到所要查找的字符串长度,接着判断是不是该字符串。这里因为是反向查找,所以定位字符判断完一个向前移

		System.out.println(s.substring(2));//cdabcd
		System.out.println(s.substring(2,5));//cda
		
//		System.out.println(s.charAt(20));// 报异常StringIndexOutOfBoundsException:String index out of range: 20
		System.out.println(s.indexOf('g'));//-1 我们可以通过-1来判断该字符或者字符串是否存在
		
		stringMethodDemo1();
	}

	private static void stringMethodDemo1() {
		// TODO Auto-generated method stub
		String s;
		String s1="";
		String s2=" ";
//		System.out.println(s.length());//s只是一个字符串对象的引用,调用方法需要实例对象,所以此语句是错误的。
		System.out.println(s1.length());//0
		System.out.println(s2.length());//1 空格也算一个字符
		
	}

}
</span>

 2.转换:

  2.1将字符串变成字符串数组(字符串的切割)。

                String[]split(String regex);//涉及到正则表达式

  2.2将字符串变成字符数组。

                char[]toCharArray();

  2.3将字符串变成字节数组。

                byte[]getBytes();

  2.4将字符串中的字符进行大小写转换。

                StringtoUpperCase();//将小写字母转换成大写字母

                StringtoLowerCase();//将大写字母转换成小写字母

  2.5将字符串中的内容进行替换。

                Stringreplace(char oldChar,char newChar);

                Stringreplace(String oldStr,String newStr);

  2.6将字符串两端空格去除。

                Stringtrim();

  2.7将字符串进行连接。

                Stringconcat(String str);

  2.8将int类型转换成String类型表示。

                staticString valueOf(int i);

 

 个人总结:

 1、凡是返回String类型的方法都没有把值赋给s,而是新建了String对象。可以从倒数第四组代码看出。

 2、需要参数的方法,参数有的是char类型,有的是String类型,我觉得只要记得String类型的就可以了。

 因为参数为char的方法都有对应的参数为String的方法,并且char字符可以用字符串来体现。如'c'与"c"。

 示例:

<span style="font-size:18px;">public class StringMethodDemo2{
	public static void main(String[] args){
		String s="张三,李四,王二";
		
		String[] arrStr=s.split(",");
		for(int i=0;i<arrStr.length;i++){
			System.out.println(arrStr[i]);//张三    李四    王二
		}
		
		char[] arrCh=s.toCharArray();
		for(int i=0;i<arrCh.length;i++){
			System.out.println(arrCh[i]);//张     三  , 李     四  , 王    二
		}
		
		s="ab你";
		byte[] arrBy=s.getBytes();
		for(int i=0;i<arrBy.length;i++){
			System.out.println(arrBy[i]);//97 98 -60 -29 一个汉字包括两个字符,这里的"你"就是-60和-29
		}
		
		s="abcABC";
		System.out.println(s.toUpperCase());//ABCABC 说明只是新建了个全大写的字符串对象,并没有赋值给s
		System.out.println(s);//abcABC
		System.out.println(s.toLowerCase());//abcabc
		System.out.println(s);//abcABC
		
		System.out.println(s.replace('c','f'));//abfABC
		System.out.println(s.replace("ab","cc"));//cccABC

		s="  a bc ";
		System.out.println("-"+s+"-");//-  a bc -
		System.out.println("-"+s.trim()+"-");//-a bc-

		s="abc";
		//System.out.println(s.concat('d'));//编译出错,提示char类型无法String类型,看来此连接方法只适用String类型
		System.out.println(s.concat("d"));//abcd 但是可以在传单字符的字符串,嘿嘿,没问题了
		System.out.println(s+"d");//abcd 上面代码与此代码等同

		int i=6;
		System.out.println(String.valueOf(i)+1);//61 将int类型的6转换成String类型的"6"
		System.out.println("6"+1);//61 等同于上面代码
	}
}
</span>

 3.判断:

  3.1两个字符串内容是否相同。

                booleanequals(Object obj);

  3.2忽略大小写判断两字符串是否相同。

                booleanequalsIgnoreCase(String str);

  3.3字符串中是否包含指定字符串。

                booleancontains(String str);

  3.4字符串是否以指定字符串开头。是否以指定字符串结尾。

                booleanstartsWith(String str);

                booleanendsWith(String str);

 

  4.比较:

                intcompareTo(String str);//比较两字符串的ascii码值

  如果此字符串等于参数字符串,则返回0;

  如果此字符串小于参数字符串,则返回负数;

  如果此字符串大于参数字符串,则返回正数。

判断和比较的示例:

<span style="font-size:18px;">public class StringMethodDemo3{
	public static void main(String[] args){
		String s="abc";
		
		System.out.println(s.equals("abc"));//true
		System.out.println(s.equals("ABC"));//false
		
		System.out.println(s.equalsIgnoreCase("ABC"));//true
		
		System.out.println(s.contains("bc"));//true
		System.out.println(s.contains("bd"));//false
		
		s="ArrayDemo.java";
		System.out.println(s.startsWith("Array"));//true
		System.out.println(s.startsWith("hh"));//false
		System.out.println(s.endsWith(".java"));//true
		System.out.println(s.endsWith("hh"));//false
		System.out.println(s.contains("Demo"));//true
		
		System.out.println("abc".compareTo("aqz"));//小于0的数,运行结果为-15
	}
}</span>

另外再提了一方法intern方法

String intern();

这个方法主要是,有一个字符串池,如果池中有与调用此方法一样内容的字符串,则返回池中的字符串;

如果没有,则在池中创建一个和调用此方法的字符串一样内容的字符串,然后把创建的这个池中字符串返回。

示例:

<span style="font-size:18px;">public class StringObjectDemo{
	public static void main(String[] args){
		String s=new String("abc");//是在堆内存中创建了两个字符串对象,一个是new的,一个是创建的
		String s1=s.intern();//在字符串池中找不到有和"abc"一样内容的字符串,于是就在池中创建了一个,然后返回给了s1
		System.out.println(s1==s);//false
	}
}</span>





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值