• java基础学习日志--String、StringBuffer方法案例


    package StringDemo;
    
    import java.util.Arrays;
    
    /*
     * 常用String、StringBufer类的方法
     */
    public class Demo1 {
    	public static void main(String[] args) {
    		StringDemo();
    		StringBufferDemo();
    	}
    
    	private static void StringBufferDemo() {
    		/*
    		 * public StringBuffer append()的参数种类繁杂,包括6种内置的基本数据类型
    		 * 参数是对象时,添加的是类全名和哈希值,返回的都是一个StringBuffer对象
    		 */
    		StringBuffer sbf=new StringBuffer("Hello");
    		sbf.append(",World!");
    		System.out.println(sbf);//Hello,World!
    		sbf.append('H');
    		System.out.println(sbf);//Hello,World!H
    		sbf.append(10086);
    		System.out.println(sbf);//Hello,World!H10086
    		sbf.append(false);
    		System.out.println(sbf);//Hello,World!H10086false
    		char []cha= {'a','p','p','l','e'};
    		sbf.append(cha);
    		System.out.println(sbf);//Hello,World!H10086falseapple
    		sbf.append(1.0402);
    		System.out.println(sbf);//Hello,World!H10086falseapple1.0402
    		Object obj=new Object();
    		sbf.append(obj);
    		System.out.println(sbf);//Hello,World!H10086falseapple1.0402java.lang.Object@7852e922
    		
    		System.out.println(sbf.charAt(1));//e
    		/*
    		 * public StringBuffer deleteCharAt(int index)
    		 * 移除此序列指定位置的 char
    		 */
    		System.out.println(sbf.deleteCharAt(1));//Hllo,World!H10086falseapple1.0402java.lang.Object@7852e922
    		/*
    		 * public StringBuffer delete(int start,int end)
    		 * 移除此序列的子字符串中的字符,【start,end),左开右闭
    		 */
    		System.out.println(sbf.delete(0, 5));//World!H10086falseapple1.0402java.lang.Object@7852e922
    		/*	
    		 * public int indexOf(String str)
    		 * public int indexOf(String str,int fromIndex)
    		 * 与String不同的是,StringBuffer只有两个indexOf(),参数是String系
    		 */
    		System.out.println(sbf.indexOf("W"));//0
    		System.out.println(sbf.indexOf("H1",1));//6
    		/*
    		 * public StringBuffer insert(int offset,xxx b)
    		 * insert()方法带一个参数叫做偏移量,即offset,applend()中可以把offset看作为end-1,
    		 * xxx表示类型很多,与applend相似
    		 */
    		System.out.println(sbf.insert(1, false));//Wfalseorld!H10086falseapple1.0402java.lang.Object@7852e922
    		/*
    		 * public StringBuffer reverse()
    		 * 将此字符序列用其反转形式取代
    		 */
    		System.out.println(sbf.reverse());//229e2587@tcejbO.gnal.avaj2040.1elppaeslaf68001H!dlroeslafW
    		/*
    		 * public void setCharAt(int index,char ch)
    		 * 将给定索引处的字符设置为 ch,即修改功能
    		 */
    		sbf.setCharAt(0, 'H');
    		System.out.println(sbf);//H29e2587@tcejbO.gnal.avaj2040.1elppaeslaf68001H!dlroeslafW
    		/*
    		 * replace、subString方法不再演示,与String相似
    		 */
    	}
    
    	private static void StringDemo() {
    		String str1=new String("ABCDEFG");
    		/*
    		 * charAt(int)方法返回char,根据下标查找相应字符
    		 * 有一个对应的StringIndexOutOfBoundsException
    		 */
    		System.out.println(str1.charAt(1));//B
    		/*
    		 * indexOf(int),注意,参数为char是自动转换对应的ASCII码值
    		 * 根据参数,返回参数对应的下标值
    		 * 平时,我们多用char,但是其实没有对应的参数类型
    		 * public int indexOf(int ch,int fromIndex)
    		 * 返回从后一个参数开始计数,前一个参数的位置,indexOf(int)中默认第二个参数为0
    		 * public int indexOf(String str)
    		 * 返回str第一次出现的位置
    		 * public int indexOf(String str,int fromIndex)
    		 * 该方法返回值暂且不讨论
    		 */
    		System.out.println(str1.indexOf('B'));//1
    		System.out.println(str1.indexOf(66));//1,效果个'B'一样,猜测是因为'B'自动转化int
    		System.out.println(str1.indexOf('C',1));//2
    		System.out.println(str1.indexOf("EF"));//4
    		System.out.println(str1.indexOf("EF",2));//4
    		/*
    		 * public int length()
    		 * 返回此对象表示的字符序列的长度。与数组不同的是,这是方法,数组,如array.length是成员
    		 */
    		System.out.println(str1.length());//7
    		
    		/*
    		 * public byte[] getBytes()
    		 * 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
    		 */
    		System.out.println(Arrays.toString(str1.getBytes()));//[65, 66, 67, 68, 69, 70, 71]
    		/*
    		 * public String(byte[] bytes)
    		 * 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。 
    		 */
    		System.out.println(new String(str1.getBytes()));//ABCDEFG
    		/*
    		 * public String replace(char oldChar,char newChar)
    		 * 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
    		 */
    		System.out.println(str1.replace("ABC", "CBA"));//CBADEFG
    		/*
    		 * public String[] split(String regex)
    		 * 根据给定正则表达式的匹配拆分此字符串。,返回一个String数组
    		 */
    		System.out.println(Arrays.toString(str1.split("")));//[A, B, C, D, E, F, G],""空串将每一个字符都拆开
    		/*
    		 * public boolean contains(CharSequence s)
    		 * 判断是否含有指定的字符串,CharSequence是一个接口,String实现了这个接口
    		 */
    		System.out.println(str1.contains("BC"));//true
    		/*
    		 * public String substring(int beginIndex)
    		 * 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
    		 * 默认从beginIndex到最后的坐标
    		 * public String substring(int beginIndex,int endIndex)
    		 * 【beginIndex,endIndex),左开右闭
    		 */
    		System.out.println(str1.substring(4));//EFG
    		System.out.println(str1.substring(1, 5));//1,2,3,4--->BCDE
    		/*
    		 * public String trim()
    		 * 返回字符串的副本,忽略前导空白和尾部空白。 即开头和结尾的空白不做处理
    		 */
    		System.out.println("  abc ed gd d ".trim());//abc ed gd d
    		/*
    		 * public String toLowerCase()
    		 * 将此 String 中的所有字符都转换为小写
    		 * 相应的也存在一个toUpperCase把所有字符转化为大写
    		 */
    		System.out.println(str1.toLowerCase());//abcdefg
    		/*
    		 * public char[] toCharArray()
    		 * 将此字符串转换为一个新的字符数组。
    		 */
    		System.out.println(Arrays.toString(str1.toCharArray()));//[A, B, C, D, E, F, G]
    		/*
    		 * public boolean equals(Object anObject)
    		 * 将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。 
    		 */
    		System.out.println(str1.equals("BCDEF"));//false
    	}
    	
    }
    

  • 相关阅读:
    LeetCode--Array--Two sum (Easy)
    LeetCode--Unique Email Addresses & Hamming Distance (Easy)
    LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
    LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)
    LeetCode 11月第1周题目汇总
    LeetCode 十月份题目汇总
    【每天一题】LeetCode 172. 阶乘后的零
    【每天一题】LeetCode 121. 买卖股票的最佳时机
    【每天一题】LeetCode 0107. 自底向上层遍历二叉树
    【每天一题】LeetCode 0067. 二进制求和
  • 原文地址:https://www.cnblogs.com/sunqiangstyle/p/10312319.html
Copyright © 2020-2023  润新知