• 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));
     
    	   }
        }
    

    输出:

     
  • 相关阅读:
    Struts1、Struts2的线程安全问题
    java基础系列——线程池
    Memcached基础
    Git基本应用
    Angular UI框架 Ng-alain @delon的脚手架的生成开发模板
    .NET CORE 框架ABP的代码生成器(ABP Code Power Tools )使用说明文档
    角落的开发工具集之Vs(Visual Studio)2017插件推荐
    【52ABP实战教程】0.3-- 从github推送代码回vsts实现双向同步
    【52ABP实战教程】0.1-- Devops如何用VSTS持续集成到Github仓库!
    【52ABP实战教程】0.2-- VSTS中的账号迁移到东亚
  • 原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/7837412.html
Copyright © 2020-2023  润新知