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.
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);
String s ="123456789abcdefgh";
String sub = s.substring(0, 10);
String remainder = s.substring(10);
String newString = oldString.substring(0, 10);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With