zoukankan      html  css  js  c++  java
  • java 基础功能

       1、str.length();// 获取整个字符串的长度

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

     打印为7  abcdefg一共7个
           2、 str.trim();// 去掉字符串两边的空格

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

     “ abcdefg ”转换为“abcdefg”两头的空格没有了
           3、 str.charAt(int i);// 获取某个索引值上的字符

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

     索引为6的字符为f
           4、 str.contains(CharSequence s);// 是否包含某个字符串

    public class Test {
    	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;
           5、 str.startsWith(String s);字符串开始的字符串。
           6、 str.endsWith(String s);字符串结束的字符串

    public class Test {
    	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
           7、 replace(char o, char n);替换字符
           8、 replace(CharSequence o, CharSequence n);

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

    字符串中所有的a替换为b
           9、split(String s);拆分字符串放到数组里

    public class Test {
    	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]);
    		}
    
    	}
    }
    

     讲字符串abcdefg拆分放到数组_s[a,b,c,d,e,f,g ]中
           10、 toUpperCase();转换为大写

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

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

    public class Test {
    	public static void main(String[] args) {
    		String  s = "abcdefg";	
    				System.out.println(String.valueOf(1234));//这个使用String  和别的不一样
    	}
    }
    


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

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

    字符串abcbdfeffg第一次出现b的索引是1最后一次出现f的索引是8
           15 、 str.substring(int i);//取索引值为这个整数参数后面的字符串
           16、 str.substring(int a, int b);//取a和b之间的字符串(不包括b)*/

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

     索引3开始的字符串包括3是bdfeffg;索引3开始到5结束不包括5是bd

  • 相关阅读:
    C++ 扩展 Op
    Python 扩展 Op
    VS Code 调试 OneFlow
    运行时数据获取
    OFRecord 图片文件制数据集
    OFRecord 数据集加载
    OFRecord 数据格式
    OneFlow 并行特色
    Consistent 与 Mirrored 视角
    作业函数的定义与调用
  • 原文地址:https://www.cnblogs.com/navyouth/p/7833217.html
Copyright © 2011-2022 走看看