java 基础功能

public class Test4{
	public static void main(String[] args) {
		String  s="abcdefg";
		System.out.println(s.length());
	   }
    }

 ①上面代码:str.length();// 获取整个字符串的长度 

输出结果:(abcdefg的位数是七位。)

②str.trim();// 去掉字符串两边的空格

public class Test4{
	public static void main(String[] args) {
		String  s="  abcdefg  ";(两边都有空格)
		System.out.println(s.trim());
	   }
    }

输出结果:  (两边的空格没有了。)

③str.charAt(int i);// 获取某个索引值上的字符

public class Test4{
	public static void main(String[] args) {
		String  s="  abcdefg  ";
		System.out.println(s.charAt(6));
	   }
    }

输出结果:   (索引从0开始,前面有两个空格,也算是两个字符。)

④str.contains(CharSequence s);// 是否包含某个字符串

public class Test4{
	public static void main(String[] args) {
		String  s = "abcdefg"; 
        System.out.println(s.contains("ab"));
        System.out.println(s.contains("abf"));		
	   }
    }

输出:(字符串abcdefg中有ab字符串为true,但是没有abf为false。)

⑤ str.startsWith(String s);字符串开始的字符串
⑥str.endsWith(String s);字符串结束的字符串

public class Test4{
	public static void main(String[] args) {
		String  s = "abcdefg"; 
        System.out.println(s.startsWith("ab"));
        System.out.println(s.endsWith("g"));		

	   }
    }

输出:(abcdefg字符串中是以ab开头以fg结束所以为true)

⑦ replace(char o, char n);替换字符
⑧ replace(CharSequence o, CharSequence n);

public class Test4{
	public static void main(String[] args) {
		String  s = "abcdefg"; 
        System.out.println(s.replace("a","b"));
	   }
    }

输出:

⑨split(String s);拆分字符串放到数组里

public class Test4{
	public static void main(String[] args) {
		String  s = "a,b,c,d,e,f,g"; 
		String[]_s=s.split(",");
			for( int i = 0; i<_s.length; i++){
					System.out.println(_s[i]);
			}		
	   }
    }

输出:

⑩ toUpperCase();转换为大写

public class Test4{
	public static void main(String[] args) {
		String  s = "abcdefg"; 
        System.out.println(s.toUpperCase());
 
	   }
    }

输出:

11、toLowerCase();转换为小写
12 、valueOf(any args);讲任意参数或者对象转换为string格式输出

public class Test4{
	public static void main(String[] args) {
		String  s = "abcdefg"; 
        System.out.println(String.valueOf(1234));
 
	   }
    }

输出:

 13、 str.indexOf(String s);//取这个字符串第一次出现的索引位置
 14、 str.lastIndexOf(String s);//取这个字符串最后一次出现的索引位置

public class Test4{
	public static void main(String[] args) {
		String  s = "abcbdfeffgabcbdfeffg"; 
        System.out.println(s.indexOf("b"));
		System.out.println(s.lastIndexOf("f"));
 
	   }
    }

输出:(字符串abcbdfeffgabcbdfeffg第一次出现b的索引是1最后一次出现f的索引是18)

 15 、 str.substring(int i);//取索引值为这个整数参数后面的字符串
 16、 str.substring(int a, int b);//取a和b之间的字符串(不包括b)

public class Test4{
	public static void main(String[] args) {
		String  s = "abcbdfeffgabcbdfeffg"; 
        System.out.println(s.substring(3));
		System.out.println(s.substring(3,5));
 
	   }
    }

输出:

 
原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/7837412.html