Index Separation Line

How would I split a string into a specific index? for example, split the row into index 10, so that the row will now be equal to all values ​​up to index 10, and then reset the remainder.

+8
java
source share
5 answers

How about substring(0,10) or substring(0,11) depending on whether index 10 is included or not? You would have to check length() >= index , though.

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

+22
source share
 String s ="123456789abcdefgh"; String sub = s.substring(0, 10); String remainder = s.substring(10); 
+11
source share

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

+2
source share
 String newString = oldString.substring(0, 10); 
+2
source share

it 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

long sentence repeating itself = 1 and = 2

+2
source share

All Articles