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.
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:
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("\"","")
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:
replace String with another in java
(7 answers)
Closed 8 years ago.
I have string "2x+3" and I want change 'x' to string "8".
String operation = "2x+3";
String x = "8";
And I want result such as "2*8+3"
or
String operation = "x+3";
String x = "8";
And I want result such as "8+3"
Does anyone know how to solve ?
operation = operation.replace("x","8")
Well you can't change the same string, because strings are immutable objects in java. What you can do is use the replace function in string class and get a new string out of it.
Have a look at this question : replace String with another in java
This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.