This question already has answers here:
What are all the escape characters?
(5 answers)
Closed 2 years ago.
I read a line from a txt file containing a string = "abc,"".
If one wanted to replace string to: string = "abc", one would write
string = string.replace(",",""); to replace the comma, but how would one replace the "?
Problem:
string = string.replace(""","");//code does not work because of """
You need to escape the ", so like this:
string = string.replace("\"","")
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I have a string like this:
String ColorCodes = "colorcodes:#FFOOFF,#OOACOF,#EEAAAA"
I want to get the three color codes in the above string into three different strings, like this:
String ColorOne = "#FFOOFF"
String ColorTwo = "#OOACOF"
String ColorThree = "#EEAAAA"
How can I do this?
You can use substringand split:
String[] colors = colorCodes.substring(colorCodes.indexOf(":") + 1).split(",");
System.out.println(Arrays.toString(colors));
Output
[#FFOOFF, #OOACOF, #EEAAAA]
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a string named extractDb values can be
GDSHKG.db
GDSMNH.db
GDSTKY.db
GDSLDN.db
now i want to remove the .extension partof it
that is i want to see the result as
GDSHKG
GDSMNH
GDSTKY
GDSLDN
please advise how to achieve this in java
Try to use substring like this:
String myString = "GDSHKG.db";
String newString = myString.substring(0, myString.indexOf("."));
System.out.print(newString);
Output:
GDSHKG
This question already has answers here:
Check if String contains only letters
(17 answers)
Closed 7 years ago.
I would like to validate the String and to ensure that the String contains only alphabets.
Here is my program and else part is getting printed
String myString = "hellothisisastring";
String reg="[a-zA-Z]";
if(myString.matches(reg))
{
System.out.println("Yep!");
}
else
{
System.out.println("Nope!");
}
Try to change your regex like
String reg="[a-zA-Z]+";
+ will ensure that it matches string one and more time.
This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .
This question already has answers here:
How to enter quotes in a Java string?
(10 answers)
Closed 8 years ago.
how can i create a String object from literal ? e.g. i have
String s = " `<div id="top_bin">` ";
this gives an error syntax error on token
You have to escape the quotes used inside the String. Use \" for that:
String s = " <div id=\"top_bin\"> ";
^ ^