字符串操作

package string;

public class StringMethodDemo
{
	public static void method_others()  //转换大小写,去除空格,顺位比较
	{
		String s="      hello java      ";
		
		sop(s.toUpperCase());
		sop(s.toLowerCase());
		sop(s.trim());
		
		String s1="aaa";
		String s2="a1c";
		
		sop(s1.compareTo(s2));
	}
	public static void method_sub()  //获取字符串中一部分
	{
		String s="abcdef";
		
		sop(s.substring(2)); //从指定位置开始到结尾
		sop(s.substring(2,4));  //包含头,不包含尾
		sop(s.substring(0,s.length()));   //全字符串
	}
	public static void method_split()  //切割
	{
		String s="3333,7777,1111";
		String[] arr=s.split(",");
		for(int x=0;x<arr.length;x++)
		{
			sop(arr[x]);
		}
	}
	public static void method_replace()  //替换
	{
		String s="hello java";
		
		//String s1=s.replace('a','n');
		
		String s1=s.replace("java","world");
		sop("s="+s);
		sop("s1="+s1);
	}
	public static void method_trans()  //字符串↔字符转换
	{
		char[] arr={'a','b','c','d','e','f'};
		String s=new String(arr,1,3);
		sop("s="+s);
		String s1="asdfghjkl";
		char[] chs=s1.toCharArray();
		for(int x=0;x<chs.length;x++)
		{
			sop("ch="+chs[x]);
		}
	}
	public static void method_is()  //判断
	{
		String str="ArrayDemo.java";
		
		sop(str.startsWith("Array"));
		sop(str.endsWith(".java"));
		sop(str.contains("Demo"));
	}
	public static void method_get()  //获取字符串
	{
		String str="asdfghjkl;";
		
		sop(str.length());  //
		sop(str.charAt(3));
		sop(str.indexOf('a',3));
	}
	public static void main(String[] args)
	{
		//method_get();
		//method_is();
		//method_trans();
		//method_replace();
		//method_split();
		//method_sub();
		method_others();
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

  

posted @ 2019-04-14 20:31  君一  阅读(134)  评论(0)    收藏  举报