This question already has answers here:
How do I convert strings between uppercase and lowercase in Java?
(6 answers)
Closed 6 years ago.
Here is a string
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
I was wondering (although it is currently one in lower case)
What code would i use to make any string into Just lower or upper case
Many thanks
What about the String toUpperCase() Method ?
String newString = foxes.toUpperCase()
See http://www.tutorialspoint.com/java/java_string_touppercase.htm
There's a method in the String class:
String foxes = "the,quick,brown,fox,jumped,over,the,lazy,dog";
String upperCase = foxes.toUpperCase();
String lowerCase = foxes.toLowerCase();
String is immutable, so you don't alter the original; you create new ones.
Related
This question already has answers here:
How to convert CharSequence to String?
(6 answers)
Closed 2 years ago.
Suppose I want to convert a CharSequence to a String in Java.
Which option (1 or 2) is better and why?
CharSequence source = "some text";
String someText1 = (String)source; // 1
String someText2 = source.toString(); // 2
The best option is behind Door #3:
String someText = String.valueOf(source);
Because that will handle the case where source is null.
#1 doesn't even work for CharSequence implementations other than String, so #2 is really your only option.
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:
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:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I am able to capture a file name in either a File or String format.
For example the file paths.
\EAM\testing
\EAM\development
\System\Applications\Management
I need to break these into different strings or substring so they would be
\EAM\testing
String 1: EAM
String 2: testing
\EAM\development
String 1: EAM
String 2: development
\System\Applications\Management
String 1: System
String 2: Applications
String 3: Management
For the first two I know i could possibly use
int index = myStr.lastIndexOf('#');
String firstPart = myStr.substring(1, index);
Any help is greatly appreciated.
String[] parts = myStr.split("\\");
Use String.split() method to get all the values in an array:
String []names = filePathString.split("\\");
for(String name:names) {
System.out.println("name: " + name);
}
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.