AlgorithmExample6

    xiaoxiao2021-03-25  171

    算法练习 (之六)

    1.字符串分割;

    2.获取字符串中特定字符位置;

    /** * 计算字符串最后一个单词的长度 */ package pers.benjamin.example; public class AlgorithmExample6 { public static String str = "Hello world"; public static void main(String[] args) { countMethod1(str); countMethod2(str); } public static void countMethod1(String str) { String[] subStr = str.split(" "); System.out.println("First Method:"); System.out.println(subStr[subStr.length-1].length()); } public static void countMethod2(String str) { System.out.println("Second Method:"); System.out.println(str.length()-1-str.lastIndexOf(" ")); } }
    转载请注明原文地址: https://ju.6miu.com/read-4445.html

    最新回复(0)