Posts

Showing posts with the label Retaining split character in the resulting parts

Java Split method, Handling special characters in Split method, Retaining split character in the resulting parts

Java Split method, Handling special characters in Split method, Retaining split character in the resulting parts String string = "004-034556"; String[] parts = string.split("-"); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556 Note that this takes a regular expression, so remember to escape special characters if necessary. there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters". So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use...