Splitting a space separated list

前端 未结 8 1091
暖寄归人
暖寄归人 2021-01-04 09:01

This is a common task I\'m facing: splitting a space separated list into a head element and an array containing the tail elements. For example, given this

相关标签:
8条回答
  • 2021-01-04 09:35
    str= "the quick brown fox"
      pqr = str.split(" ")
    
    0 讨论(0)
  • 2021-01-04 09:42
    public static void main(String[] args) {
            String s = "the quick brown fox";
            int indexOf = s.indexOf(' ');
            String head = s.substring(0, indexOf);
            String[] tail = s.substring(indexOf + 1).split(" +");
            System.out.println(head + " : " + Arrays.asList(tail));
        }
    

    Would have been much easier in Haskell :)

    0 讨论(0)
提交回复
热议问题