zoukankan      html  css  js  c++  java
  • Java String 常用类的使用方法

    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    /*		
     * 使用for循环遍历数组
     * int arr[]=new int[]{};
    		int arr1[][]={{1,2,3,4,5,6,7,8,9},{2,3,6,8,7},{8,5,9,6,7}};
    		for (int i = 0; i < arr1.length; i++) {
    			for (int j = 0; j < arr1[i].length; j++) {
    				int content=arr1[i][j];
    				System.out.print(content);
    			}
    			System.out.println("
    -----------------------------------------------------");
    		}*/
    		String arr[]={"1","2","3","4","5","3","9"};
    		//
    		//String text="hello.txt;study.java;word.php;hello.word.java;lastnone.c;";
    		//System.out.println(text.indexOf("t"));//返回第一次出现t的索引,---6
    		//System.out.println(text.lastIndexOf("a"));//返回字符串最后一次出现的a的索引位置--47
    		//System.out.println(text.lastIndexOf(".java"));//返回字符串最后一次出现的.java的索引位置--47
    		//char  ch= text.charAt(4);//返回一个char类型的字符串
    		//String re=ch+"";
    		//System.out.println(re.toUpperCase());//toUpperCase();把小写字母转成大写字母,返回类型为String类型
    		//System.out.println(text.charAt(4));//返回字符串索引值为4的那一个字符为o
    		//System.out.println("字符串的长度为:"+text.length());
    		//-----------------------------------------------------------------------------------------------------------------------
    		//找出字符串中以.java结尾的字符串,分隔符为;
    		String text="hello.txt;study.java;word.php;hello.word.java;lastnone.c;";
    		String rest[]=text.split(";");//用split方法去分割字符串,返回的是一个string数组,必须用一个String数组去接收
    		for (String i : rest) {
    			int  re=i.lastIndexOf(".java");
    			if(re>0){//判断每一个字符串是否有.java结尾的,有就输出,没有就跳过进入下一次循环
    				System.out.println(i);
    			}else{
    				continue;
    			}
    		 }//用foreach循环语句去遍历数组,for(元素变量 x:遍历的对象 obj){引用x的java语句}
    		//-----------------------------------------------------------------------------------------------------------------------
    	}
    
    /*	判断字符串为空为null或者为空字符串*/
    	private static boolean isBlank(String str){
    		return !hasLength(str);
    	}
    /*	
     * 判断字符串非空
     * 判断字符串不空,既不是引用为null,也不是空字符
     * */
    	private static boolean hasLength(String str){//把这两个判断字符串为空的方法封装到一类里面,就可以通用了
    //		if(str !=null && "".equals(str.intern()) ){
    //			return true;
    //		}
    //		return false;
    		return str !=null && "".equals(str.intern());
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    /*		
     * 使用for循环遍历数组
     * int arr[]=new int[]{};
    		int arr1[][]={{1,2,3,4,5,6,7,8,9},{2,3,6,8,7},{8,5,9,6,7}};
    		for (int i = 0; i < arr1.length; i++) {
    			for (int j = 0; j < arr1[i].length; j++) {
    				int content=arr1[i][j];
    				System.out.print(content);
    			}
    			System.out.println("
    -----------------------------------------------------");
    		}*/
    		String arr[]={"1","2","3","4","5","3","9"};
    		//
    		//String text="hello.txt;study.java;word.php;hello.word.java;lastnone.c;";
    		//System.out.println(text.indexOf("t"));//返回第一次出现t的索引,---6
    		//System.out.println(text.lastIndexOf("a"));//返回字符串最后一次出现的a的索引位置--47
    		//System.out.println(text.lastIndexOf(".java"));//返回字符串最后一次出现的.java的索引位置--47
    		//char  ch= text.charAt(4);//返回一个char类型的字符串
    		//String re=ch+"";
    		//System.out.println(re.toUpperCase());//toUpperCase();把小写字母转成大写字母,返回类型为String类型
    		//System.out.println(text.charAt(4));//返回字符串索引值为4的那一个字符为o
    		//System.out.println("字符串的长度为:"+text.length());
    		//-----------------------------------------------------------------------------------------------------------------------
    		//找出字符串中以.java结尾的字符串,分隔符为;
    //		String text="hello.txt;study.java;word.php;hello.word.java;lastnone.c;";
    //		String rest[]=text.split(";");//用split方法去分割字符串,返回的是一个string数组,必须用一个String数组去接收
    //		for (String i : rest) {
    //			int  re=i.lastIndexOf(".java");
    //			if(re>0){//判断每一个字符串是否有.java结尾的,有就输出,没有就跳过进入下一次循环
    //				System.out.println(i);
    //			}else{
    //				continue;
    //			}
    //		 }//用foreach循环语句去遍历数组,for(元素变量 x:遍历的对象 obj){引用x的java语句}
    //		String content=text.substring(0,5);//substring(int beginIndex, int endIndex)截取字符串方法.不写第二个参数,截取字符串一直到最后。
    //		System.out.println(content);
    		//-----------------------------------------------------------------------------------------------------------------------
    		//判断字符串是否为空
    		String content1="s";
    		Boolean rest1=hasLength(content1);
    		System.out.println(rest1);
    	}
    

  • 相关阅读:
    LeetCode——面试题57
    翻译——5_Summary, Conclusion and Discussion
    LeetCode——114. 二叉树展开为链表
    LeetCode——1103. 分糖果 II
    LeetCode——337. 打家劫舍 III
    LeetCode——994. 腐烂的橘子
    Python——潜在会员用户预测
    Vue中div高度自适应
    webpack中使用vue-resource
    Mint UI组件库 和 Mui
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/6658295.html
Copyright © 2011-2022 走看看