removing underscore form a string [duplicate] - java

This question already has answers here:
Replacing characters in Java
(6 answers)
Closed 8 years ago.
I have a string that looks like this
abcd= "name_person"
but I would like it to read like this:
abcd= "nameperson"
how can I do this with Java.

abcd = abcd.replaceAll("_", "");

you should do:
abcd = abcd.replaceAll("_", "");
This will replace all occurrences of underscore with nothing, so you are left without underscores.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

You can use the string replace function:
abcd.replace("_", "");

Related

How to replace the end of a string? [duplicate]

This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?
Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string

Split a string into substring using '|' character [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 3 years ago.
I am trying to split a string as follows, "Client|Role". My regex is "|".
I expected two substrings as "Client" and "Role", but what I get is each and every character as a substring including the character in my regex. Why I am getting like this? Please help, Thanks in advance.
My code is as follows,
String output = "Client|Role";
String values[] = output.split("|");
You can split like this..
String b = "Client|Role";
String[] elements = b.split("\\|");

java.lang.ArrayIndexOutOfBoundsException after two splits [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I want to split a string three times.
This is the string:
21.06.2016;00:30
My function looks like this:
String[] split = dateV.split(";");
String[] date = split[0].split(".");
String[] time = split[1].split(":");
date[0] should contain "21" after all
So the first part works great.
My two strings are
split[0] = 21.06.2016
split[1] = 00:30
But when I call split[0].split("."); I get a
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
Can somebody tell me why?
String.split uses regular expressions to do the split, and a dot is a special character when regular expressions are used.
To split using a dot, you need to escape it like this
String[] date = split[0].split("\\.");

Java Spit "." cannot work [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I try to split "." but it can not work, I get strings.length equals 0 .What's wrong with it?
String string = "11.12.1";
String[] strings = string.split(".");
As string split takes an regex as argument, . is a wildcard for any character. Just escape it using a backslash (which you have to also escape for java with another one). Additionally, as Youcef Laidani pointed out, you have to call split on the string you just created, not something else:
string.split("\\.");

split string not working in java [duplicate]

This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Split a String on | (pipe) in Java [duplicate]
(3 answers)
Closed 8 years ago.
I'm trying to split the following: 116,24|426,776 into two strings upon the | symbol.
My code:
String[] splitString = "116,24|462,776".split("|");
System.out.println(splitString[1]);
However, this code simply returns: 1
Any clues?
You have to use escape character.
Try this String[] splitString = "116,24|462,776".split("\\|");
Here \\ is the escape character.
Please refer http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Categories

Resources