How to Split a Java String at Backslash

Java backslash split string

\002 represents a unicode character. SO i suggest you to split your input according to the character other than alphabet or digit.

String string1 = "test\00216243";
String part[] = string1.split("[^a-z0-9]");
System.out.println(part[0]);

Output:

test

How to split string at single Backslash java

Don't split. Just parse:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy\\MM\\dd");
LocalDate dPresent = LocalDate.parse(datum, formatter);

How to use String#split with a backslash character?

In regex-land, a \ is an escape character, so to obtain a literal \ we need to escape it: \\. However, in Java strings, \ is also an escape character, so we need to escape each \ a second time, resulting in \\\\. Therefore, this is what you want:

str.split("\\\\")

How do I split a string in Java?

Use the appropriately named method String#split().

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

Note that split's argument is assumed to be 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".

For instance, to split on a period/dot . (which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.

To test beforehand if the string contains certain character(s), just use String#contains().

if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}

Note, this does not take a regular expression. For that, use String#matches() instead.

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split() method.

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42

Java split String at |

Please, escape the character:

String[] parts = match.split("\\|");

How to split a string at only . and not if it is preceded by double slash?

You want to split a string with a dot not immediately preceded with // string.

Use

.split("(?<!//)\\.")

See the regex demo

The (?<!//) is a negative lookbehind that fails the match if there is a // text immediately to the left of the current location.

Java - Splitting string by backslash (\)

You say you want to split by backslash \ but you're passing in forward slash / to your test case. How can you expect it to work?!

Try this

public static void main(String[] args) {
Task myTask = new Task();
myTask.setDate("02\\03\\20");
System.out.println(myTask.dateIsValid());
Task myTask2 = new Task();
myTask2.setDate("23\\45\\6001");
System.out.println(myTask2.dateIsValid());
}


Related Topics



Leave a reply



Submit