java练习

统计字符串中数字个数如:“abc21b416u” ===> 输出5

public class TestDemo02 {
    public static int isNumeric(String str){
        int number=0;
        int n=str.length();//字符串的长度
        for (int i = 0; i =48&&str.charAt(i)<=57){//判断是否在0到9之间如果是number++
                number++;
            }
        }
        return number;
    }
    public static void main(String[] args) {
        String str="abc21b416u";
        int number=isNumeric(str);//传参
        System.out.println(number);//打印
    }
}

*题目:=====I love study 逆置为:study love I*

public class TestDemo03 {
        public static String reverseSentence(String str) {
        String[] arr=str.split(" ");
        StringBuilder stringBuilder=new StringBuilder();

        for (int i =arr.length-1; i>0 ; i--) {
              stringBuilder.append(arr[i]+"");
        }
        stringBuilder.append(arr[0]);//最后一个单词不加空格
        System.out.println(stringBuilder.toString());
        return str;
        }
        public static void main(String[] args) {
            String str="I love study";
            reverseSentence(str);
        }

}

将一个数组从左开始第几位之前的进行旋转:左旋数组
如:将"abcdef"第2位之前(a为0号位置)进行旋转----》"cdefab"

public class TestDemo04 {
    public static void leftRotateString(String str, int n) {
        char[] arr = str.toCharArray();//将字符串数组化
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = n; i <= arr.length - 1; i++) {
            stringBuilder.append(arr[i]);    //将数组吧第n号下标变成第0号下标。
        }
        for (int i = 0; i < n; i++) {
            stringBuilder.append(arr[i]);//将以前第0到n号下标打印在后面
        }
        System.out.println(stringBuilder);//打印
    }

    public static void main(String[] args) {
        String str = "abcdef";
        int n = 2;
        leftRotateString(str, n);//调用
    }
}

你可能感兴趣的:(java练习)