split string at index

前端 未结 5 2040
不知归路
不知归路 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: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

提交回复
热议问题