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

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

Related

Getting substrings from a string in Java [duplicate]

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]

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

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.

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);
}

Java - simple string issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
string split in java
I have a string:
"jack#james#joahn#logan#"
I know there is a method of the String class that can seperate it on the symbol #. So it will return to me
"jack" "james" "joahn" "logan"
What is the name of this method?
This is String.split("#").
For example:
String s = "jack#james#joahn#logan#";
String parts[] = s.split("#");
See the String.split method. Here is an example:

Categories

Resources