Getting substrings from a string in Java [duplicate] - java

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]

Related

How do I replace the " character in Java? [duplicate]

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("\"","")

getting the string value before dot in string through java [duplicate]

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

Validate that string contains only alphabets [duplicate]

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.

Extract numbers from a string in java [duplicate]

This question already has answers here:
Extract digits from a string in Java
(14 answers)
Closed 4 years ago.
I want to extract numbers from a string like:
str="good_mor9ni13ng_23guys ";
The output should be an array [9,13,23].
How should I proceed?
You could do it like this:
List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("[0-9]+").matcher("good_mor9ni13ng_23guys");
while (m.find()) {
matches.add(m.group());
}

Java split function [duplicate]

This question already has answers here:
split string on comma, but not commas inside parenthesis
(3 answers)
Closed 9 years ago.
Hi I have a question about java split
ItemName,Item,,(width,length,height),12
I want String[] equals this"
String[] result ={"ItemName","Item","","(width,length,height)","12"};
Who can help me with using split function or other regex?
If your input string is named istr, then
String[] result = istr.split(",");
should work.
try this
String data="ItemName,Item,,(width,length,height),12";
String[] result=data.split("//,");
for(String string : result) {
System.out.println(string);
}

Categories

Resources