String类的常用方法总结

package com.cghu.day07;

public class StringDemo {

	public static void main(String[] args) {
		char cas = CharAtString("helloworld");
		System.out.println("指定索引处的 char 值:"+cas);//指定索引处的 char 值:e
		
		int cts = CompareToString("ab", "a");
		System.out.println("按字典顺序比较两个字符串:"+cts);//按字典顺序比较两个字符串:1
		
		String cs = ConcatString("hello", "world");
		System.out.println("指定字符串连接到此字符串的结尾:"+cs);//指定字符串连接到此字符串的结尾:helloworld
		
		Boolean ctas = ContainsString("helloworld", "h");
		System.out.println("字符串包含指定的 char 值序列时返回 :"+ctas);//字符串包含指定的 char 值序列时返回 :true
		
		boolean ews = EndsWithString("helloworld", "orld");
		System.out.println("字符串是否以指定的后缀结束:"+ews);//字符串是否以指定的后缀结束:true
		
		int ios = IndexOfString("helloworld", 'h');
		System.out.println("字符在此字符串中第一次出现处的索引:"+ios);//字符在此字符串中第一次出现处的索引:0
		
		boolean isEmptys = IsEmptyString("");
		System.out.println(isEmptys);//true
		
		boolean matchs = MatchesString("abc", "[a-z]{3}");
		System.out.println(matchs);//true
		
		String rs = ReplaceString("I like ios","ios","java");
		System.out.println(rs);//I like java
		
		String[] splits = SplitString("hell ow orl d"," ");
		System.out.println(splits);//[Ljava.lang.String;@7f12f614
		
		boolean startsting = StartsWithString("helloworld","he");
		System.out.println(startsting);//true
		
		String sub = SubString("helloworld",3);
		System.out.println(sub);//loworld
		
		char[] tcas = ToCharArray("helloworld");
		System.out.println(tcas);//helloworld
		
		String tlcsString = ToLowerCaseString("HelloWorlD");
		System.out.println(tlcsString);//helloworld
		
		String trimstr = trimString("  helloworld    ");
		System.out.println(trimstr);//helloworld
		
		String tucs = ToUpperCaseString("helloworld");
		System.out.println(tucs);//HELLOWORLD
	}
	
	/**
	 * 第一个方法:charAt(int index) 返回指定索引处的 char 值。返回值类型为char
	 */
	public static char CharAtString(String str){
		return str.charAt(1);
	}
	
	/**
	 * 第二个方法:compareTo(String anotherString)按字典顺序比较两个字符串。 返回值类型为int
	 * str1>str2,返回的是1;str1=sr2,返回的是0;str1<str2,返回的是-1
	 */
	public static int CompareToString(String str1,String str2){
		return str1.compareTo(str2);
	}
	
	/**
	 * 第三个方法:concat(String str) 将指定字符串连接到此字符串的结尾。返回值类型为string
	 * 
	 */
	public static String ConcatString(String str1,String str2){
		return str1.concat(str2);
	}
	
	/**
	 * 第四个方法:contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。返回值类型为boolean
	 */
	public static boolean ContainsString(String str1,String str2){
		return str1.contains(str2);
	}
	
	/**
	 * 第五个方法:endsWith(String suffix) 测试此字符串是否以指定的后缀结束。返回值类型为boolean
	 */
	public static boolean EndsWithString(String str1,String str2){
		return str1.endsWith(str2);
	}
	
	/**
	 * 第六个方法:indexOf(int ch)  返回指定字符在此字符串中第一次出现处的索引。
	 */
	public static int IndexOfString(String str1,char c1){
		return str1.indexOf(c1);
	}
	
	/**
	 * 第七个方法:isEmpty() 当且仅当 length() 为 0 时返回 true。
	 */
	public static boolean IsEmptyString(String str){
		return str.isEmpty();
	}
	
	/**
	 *第八个方法: matches(String regex) 告知此字符串是否匹配给定的正则表达式。
	 */
	public static boolean MatchesString(String str1,String str2){
		return str1.matches(str2);
	}
	
	/**
	 * replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
	 */
	public static String ReplaceString(String str1,String oldstr,String newstr){
		return str1.replace(oldstr, newstr);
	}
	
	/**
	 * split(String regex) 
          根据给定正则表达式的匹配拆分此字符串。
	 */
	public static String[] SplitString(String str1,String Str2){
		return str1.split(Str2);
		
	}
	
	/**
	 * startsWith(String prefix) 
          测试此字符串是否以指定的前缀开始。
	 */
	public static boolean StartsWithString(String str1,String str2){
		return str1.startsWith(str2);
	}
	
	/**
	 * substring(int beginIndex) 
          返回一个新的字符串,它是此字符串的一个子字符串。
	 */
	public static String SubString(String str1,int str2){
		return str1.substring(str2);
	}
	
	/**
	 * toCharArray() 
          将此字符串转换为一个新的字符数组。
	 */
	public static char[] ToCharArray(String str){
		return str.toCharArray();
	}
	
	/**
	 * toLowerCase() 
          使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
	 */
	public static String ToLowerCaseString(String str){
		return str.toLowerCase();
	}
	
	/**
	 * trim() 
     * 返回字符串的副本,忽略前导空白和尾部空白。
	 */
	public static String trimString(String str){
		return str.trim();
	}
	
	/**
	 * toUpperCase() 
     * 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
	 */
	public static String ToUpperCaseString(String str){
		return str.toUpperCase();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值