I'm fairly new to java and was wondering if there is a method that finds the uppercase letters in a string and also if there is method that finds the numbers in a string. Thank you
-
1yes there is but it depends on what you want to do with those characters which determines which of the many ways to do this is best. Why are you doing this? – Peter Lawrey Jan 24 '14 at 14:40
-
Regex may help you. You could also create your own method to manual look for these – But I'm Not A Wrapper Class Jan 24 '14 at 14:41
-
What exactly your requirement is? – Tabrej Khan Jan 24 '14 at 14:41
-
A simple Google search will answer your question: http://stackoverflow.com/questions/7957944/search-for-capital-letter-in-string – Alexis Leclerc Jan 24 '14 at 14:43
4 Answers
No, it is not a proprietary method.
You can use regular expression, or loop over letters in string and extract you need.
See some code here:
Extract digits from string - StringUtils Java
Extract digits from a string in Java
Please search first, before you ask a question.
The Java String API doesn't offer a method to do that specifically. You'll have to code it yourself using some of the methods String
provides.
If you're a begginer, I'd suggest taking a look at Manipulating Strings in Java, also how do loops work in Java, and the Character
class (specifically the isDigit()
and isUpperCase()
methods).

- 27,550
- 11
- 97
- 161
You can try
for(Character c : s.toCharArray()){
if(c.toString().toUpperCase().equals(c.toString())){
System.out.println("Upper case");
}
else {
System.out.println("Lower case");
}
}
So we convert String to character array, iterate over it and in each iteration we compare itself with it's the upper case to check what case is used.
and for finding number you can do something like below
for(Character c : s.toCharArray()){
try{
Integer.parseInt(c.toString());
System.out.println("Integer Identified");
}catch (NumberFormatException ex){
System.out.println("Not an integer");
}
}
For number simply parse String using Integer.parseInt()
. If it is able to successfully parse then it is a number or if exception is thrown then it is not a number.

- 66,731
- 38
- 279
- 289
-
11. Explain what the code does. 2. Answer the rest of his questions. – But I'm Not A Wrapper Class Jan 24 '14 at 14:45
-
Since you haven't specified any plans for further processing the String, you can use something like this to find uppercase and digits:
public class Extraction {
public static void main(String[] args) {
String test = "This is a test containing S and 10";
for (char c : test.toCharArray()) {
if (Character.isDigit(c)) {
System.out.println("Digit: " + c);
}
else if (Character.isUpperCase(c)) {
System.out.println("Uppercase letter: " + c);
}
}
}
}

- 1,574
- 2
- 12
- 14