黑马程序员—String 小结

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! --------------------

一、String方法介绍

String是一个特殊的字符串,字符串一旦被初始化就不可以被改变 如以下例子

String s=”abc”;

s=”kk”改变的知识s的指向字符串abc一被定义就不可以被改变

String s1=new String(“abc”)

ss1的区别就是第一个代表一个对象,s1是代表两个对象。

String的一些常见操作

1、获取

(1)根据位置获取位置上的某个字符

char charAt(int  index)

(2)根据字符获取该字符在字符串中的位置

int indexOf(int ch)

(3)获取从指定位置字符第一出现的位置

int indexOf(int ch  int  fromIndex)

 

(4)返回字符在字符串中第一次出现的位置

Int indexOf(String str)

 

(5)返回从指定位置查找字符在字符串中第一次出现的位置

int indexOf(String str  int  fromIndex)

 

(6)返回反向索引一个字符出现的位置

int lastIndexOf(int  ch)

2、判断

(1)判断字符中是否包含某一个子串

boolean contains(str)

(2)字符串中是否有内容

boolean isEmpty();

(3)字符中是否一指定内容开头

boolean startWith(str);

 

(4)字符串是否以什么结尾

boolean endWith();

(5)判断字符串的内容是否相同

Boolean  equals(str2)

(6)判断字符串的内容是否相同并且不区分大小写

boolean equalsIgnoreCase(str1)

3、转换

(1)将字符数组转换成字符串

 String(char[] )

 String(char[] ,offsert,count)取字符数组的一部分转换成字符串

调用String中的copyValueOf(char[]);

(2)将字符串转成字符数组

char toCharArrary()

(3)将字节数组转换成字符串

String[byte[]];

String(byte[] ,offsert,count)

(4)将字符串转成字节数组

byte[] getBytes();

(5)将基本数据类型转换成字符串

static String valueOf(数据类型);

4、替换

String repalace(oldchar,newchar)

5、切割

String[] split(regex)

6、获取字符串的一部分

String sunbString(begin ,end)//包含头不包含尾

7、去除空格

String trim()去除两端空格

8、自然比较两个字符串

int compareTo(str);

下面是一个这些方法的常见操作

public class StringDemo
{
		
	public static void main(String[] args)
	{
		//Method1();
		//Method2();
		//replace();
		//split();
		//method_trim();
		method_reverse();
		}
	/*反转字符串*/
	public static void method_reverse()
	{
		String s="hlwhjasd";
		method_reverse(0,s.length()-1);//从头到尾都反转
		
	}
	
	/*去除两端空格*/
	public static void method_trim()
	{
		String str="    abcd    ";
		/*String str1=str.trim();
		sop(str1);*/
		int start=0, end=str.length()-1;
		while(start<=end&&str.charAt(start)==' ')
			start++;
		while(start<=end&&str.charAt(end)==' ')
			end--;
		sop(str.substring(start,end+1));//substring(start,end)取两端字符串,包含头,不包含尾端
			
	}
	public static void replace()
	{
		String s="ddfddtyjkll";
		String s1=s.replace('d','g');
		String s2=s.replace("ddf", "ggs");
		sop(s);//输出的还是原来的字符串,因为字符串一被初始化就不可以被改变
		sop(s1);
		sop(s2);
		
	}
	public static void split()
	{
		String s="Hello world";
		String arr[]=s.split(" ");
		for(int x=0;x<arr.length;x++)
		{
			sop(arr[x]);
		}
		
	}
	public static void Method2()
	{
		char[] arr={'q','e','s','d','v','n'};
		//将字符数组转换成字符串
		String s1=new String(arr,3,2);
		sop(s1);
		//调用String里面的静态方法copyValueOf(;
		String s2=String.copyValueOf(arr);
		sop(s2);
		
		//将字符串转化成字符数组
		String s="sfjdsljhhssl";
		char[] chs=s.toCharArray();
		for(int x=0;x<chs.length;x++)
		{
			sop(chs[x]);
		}
		
		//将字节数组转换成字符串
		byte[] by={'s','d','h','f','m'};
		String ss=new String(by);
		sop(ss);
		
		//将字符串转成字节数组
		String sss="afjdsljhhssl";
		byte[] byt=sss.getBytes();
		
		for(int x=0;x<byt.length;x++)
		{
			sop(byt[x]);
		}
		//将基本数据类型转换成字符串
		int m=3;
		sop(String.valueOf(m));
	}
		


	public static void Method1()
	{
		try{
			String str="sfoyhnhgs";
			String str1="sfoYhnhgs";
		
			
			//获取长度
			sop(str.length());
			
			//根据位置获取某个位置上的字符
			sop(str.charAt(2));
			
			//根据字符获得该字符所在的位置
			sop(str.indexOf('f'));
			
			//获取从指定位置字符第一出现的位置
			sop(str.indexOf('s',4));
			
			//返回反向索引一个字符出现的位置
			sop(str.lastIndexOf('s'));
			sop(str.lastIndexOf("h"));//int ch
			
			//判断字符中是否包含某一个子串
			sop(str.contains("sssss"));
			
			//字符串中是否有内容
			sop(str.isEmpty());
			
			//字符中是否一指定内容开头
			sop(str.startsWith("s"));
			
			//字符串是否以什么结尾
			sop(str.endsWith("s"));
			
			//判断字符串的内容是否相同
			sop(str.equals(str1));
			
			//判断字符串的内容是否相同并且不区分大小写
			sop(str.equalsIgnoreCase(str1));
		}
			
			catch(Exception e)
			{
				System.out.println("角标越界");
			}
			
			
}
	

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

二、StringBuilder 是一个字符串缓冲区是一个容器

 * 特点:他的长度是可以变化 的

 * 可以添加不同的数据类型

 * 最后都通过tostring转换成字符串

 * 

 * */

public class StringBuilder Demo {
	//存储(添加和插入)
	public static void append_method()//将指定数据添加到已有数据的尾出
	{
		StringBuilder sbu=new StringBuilder ();
		sbu.append("as").append(3).append('a');
		sop(sbu.toString());
	}
	public static void insert_method()
	{
		StringBuilder sbu=new StringBuilder ("abcdh");
		sbu.insert(2,"xx");
		sbu.insert(2,3);
		sop(sbu.toString());
		
	}
	//删除
	public static void del_method()
	{
		StringBuilder sbu=new StringBuilder("abcdh");
		sbu.delete(2, 4);
		sbu.deleteCharAt(2);//删除指定位置的字符。
		sop(sbu.toString());
	}
	//获取
	public static char get_method()
	{
		StringBuilder sbu=new StringBuilder("abcddwddh");
		
		return sbu.charAt(0);
		
		
	
}
	public static int getindexof()
	{
		StringBuilder sbu=new StringBuilder("abcddwddh");
		//return sbu.indexOf("a");
		return sbu.lastIndexOf("d");
		
		
		
	}
	
	public static void subString_method()
	{
		StringBuilder sbu=new StringBuilder("abcddwddh");
		//sbu.substring(3, 5);
		sop(sbu.substring(3, 5).toString());
	}
	//修改
	public static void update()
	{
		StringBuilder sbu=new StringBuilder("abcdh");
		sbu.replace(2,4,"fg");
		
		sop(sbu.toString());
		sop("--------------");
		sbu.setCharAt(2,'b');
		sop(sbu.toString());
	}
	//反转
	public static void reverse_method()
	{
		StringBuilder sbu=new StringBuilder("abcdh");
		sop(sbu.toString()+"....");
		sbu.reverse();
		sop(sbu.toString());
	}
	//将缓冲区中的指定数据存到字符数组中
	public static void getChars_method()
	{
		char[] chs=new char[5];
		StringBuilder sbu=new StringBuilder("abcdh");
		sbu.getChars(1, 4, chs, 1);
		for(int i=0;i<chs.length;i++)
		{
			sop("chs["+i+"]="+chs[i]);	
	}

		
	}
	private static void sop(Object obj) {
		System.out.println(obj);
		
	}
	public static void main(String[] args)
	{
		//insert_method();
		//append_method();
		del_method();
		//sop(get_method());
		//sop(getindexof());
		//subString_method();
		//update();
		//reverse_method();
		//getChars_method();
	}

}
例子:

需求:求两串字符串的最大相同子串
思路:
 * 将短的字符串按长度递减的方式获取字串
 * 将获取到的子串与长串比较,如果长串中包含则找到最大相同的字串、

public class getMaxsubstring {
	public static String getmaxString(String s1,String s2)
	{
		//判断哪个字符串比较小
		String max=" ", min=" ";
		max=(s1.length()>s2.length())?s1:s2;
		min=(max==s1)?s2:s1;
		sop("max="+max+".............min="+min);
		//获取较短字符串的子串
		for(int x=0;x<min.length();x++)
		{
			for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++)
			{
				String temp=min.substring(y,z);
				
				if(s1.contains(temp))
					return temp;
					
			}
			
		}
		return "";
		
		
	}
	public static void sop(String str)
	{
		System.out.println(str);
	}

	public static void main(String[] args)
	{
		String s1="ggytuiutHELLOiyyjppput";
		String s2="qwdfesHELLOs";
		sop(getmaxString(s2,s1));
	}
}


三、基本数据类型对象包装类

常见操作:用于基本数据类型和字符串类型之间的的转换

基本数据类型转字符串(基本数据类型+””或基本数据类型.toString(基本数据类型值))比如:Integer.toString(3);

字符串转基本数据类型

如:Integer.parseInt(String)

下面是一个例子:

  public class intergerDemo
  {
  	
  	private static void sop(Object obj) {
  	System.out.println(obj);
  		
  	}
  	public static void main(String[] args)
  	{
  		//基本数据类型转字符串
  	/*String str="sssss";
  		sop(str);
  		String x=Integer.toString(34);
  		sop(x);
  		String y=Boolean.toString(true);
  		sop(y);*/
  		//字符串转其他数据类型
  		/*int x=Integer.parseInt("123");//只能传数字格式的字符串
  		sop(x);*/
  		//Boolean b=Boolean.parseBoolean("ture");
  		//sop(b);
  		//下面是十进制转成其他进制
  		sop(Integer.toHexString(60));//把60转成16进制
  		sop(Integer.toBinaryString(60));//转成2进制
  		sop(Integer.toOctalString(60));//8进制
  		//其他进制转成十进制
  		int m=Integer.parseInt("3c",16);
  		int y=Integer.parseInt("111100",2);
  		sop("m="+m);
  		sop("y="+y);
  		
  	}
  }



------------------------ ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值