split string at index

前端 未结 5 2035
不知归路
不知归路 2020-12-06 16:08

How would I split a string at a particular index? e.g split string at index 10, making the string now equal to everything up to index 10 and then dumping the remainder.

相关标签:
5条回答
  • 2020-12-06 16:57
    String newString = oldString.substring(0, 10);
    
    0 讨论(0)
  • 2020-12-06 16:59

    this works too

    String myString = "a long sentence that repeats itself = 1 and = 2 and = 3 again"
    String removeFromThisPart = " and"
    
    myString = myString .substring(0, myString .lastIndexOf( removeFromThisPart ));
    
    System.out.println(myString);
    

    the result should be

    a long sentence that repeats itself = 1 and = 2

    0 讨论(0)
  • 2020-12-06 17:04
    String s ="123456789abcdefgh";
    String sub = s.substring(0, 10);
    String remainder = s.substring(10);
    
    0 讨论(0)
  • 2020-12-06 17:06

    What about substring(0,10) or substring(0,11) depending on whether index 10 should inclusive or not? You'd have to check for length() >= index though.

    An alternative would be org.apache.commons.lang.StringUtils.substring("your string", 0, 10);

    0 讨论(0)
  • 2020-12-06 17:07

    This should do it:s = s.substring(0,10);

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