代码之家  ›  专栏  ›  技术社区  ›  Sung Guk Lee

在理解起始索引和子字符串如何工作方面几乎不需要帮助[重复]

  •  -3
  • Sung Guk Lee  · 技术社区  · 8 年前

    这是我的密码

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
    
        System.out.print("Type a word: ");
        String userWord = reader.nextLine();
    
        System.out.print("Length of the first part: ");
        int userLength = Integer.parseInt(reader.nextLine());
    
        System.out.println("Result: " + userWord.substring(0, userLength));
    }
    

    结果:

    Type a word: hellothere
    Length of the first part: 3
    Result: hel
    

    开始索引从0开始计数,对吗?那么结果不应该打印“地狱”吗?

    1=e

    2=l

    1 回复  |  直到 8 年前
        1
  •  0
  •   Florian S.    8 年前

    的第二个参数 substring()