java - How to make substring() read a string until a certain character? -
i want create substring say:
string s1 = "derp123"; s2 = s1.substring(0, *index there's number*); is there way can achieve other using replaceall method?
i don't want replace numbers in string, want stop reading string once method detects number 0-9.
for this, regular expression friend.
i'll show code example, should learn more power (and limitations) of regular expressions. see javadoc of pattern.
string s1 = "derp123"; matcher m = pattern.compile("^\\d*").matcher(s1); if (m.find()) system.out.println(m.group()); // prints: derp note replaceall() method shown in comments using regular expression.
Comments
Post a Comment