This question already has answers here:
Java: Remove numbers from string
(6 answers)
Closed 2 years ago.
Basically my questions is how can i remove the digits from a string that is being inputted Thanks for helping me as well
Example:
Input
700N
Output:
N
Use String.replaceAll() method:
str.replaceAll("[0123456789]","");
You can use replaceAll
String st1 = "700N";
st1 = st1.replaceAll("\\d+", "");
System.out.println(st1);
output
N
The best way I could come up with :
String input ="700N";
String output= input.replaceAll("\\d","");
The regex \\d means digit.
Related
This question already has answers here:
Replacing all non-alphanumeric characters with empty strings
(14 answers)
Closed 4 years ago.
I want to get rid of all symbols or punctuation marks in a string
for example
String str = "\"hello!.?,\"";
the output of str should be: hello
You can use String's replaceAll.
String str = "\"hello!.?,\"";
String newStr = str.replaceAll("[^\\w]", "");
System.out.println(newStr);
For more details on how to construct a regex, see the documentation of Pattern.
This question already has answers here:
Java: removing numeric values from string
(7 answers)
Closed 5 years ago.
This is my string address : London, Jon 2 A
And I want to see in my output see London, Jon
I tried to do this :
String result = chapterNumber.substring(0, chapterNumber.indexOf("1"));
But I have to do 10 times from different number maybe is better way to do this
try this
String str = "London, Jon 2 A";
System.out.println(str.replaceAll("\\d.*",""));
You can use regex with groups to get only the first group (the sequence of non-digit chars until the first digit):
String result = chapterNumber.replaceAll("([^\\d])(\\d.*)", "$1");
String s = "London, Jon 2 A";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());
System.out.println(s.substring(0,s.indexOf(String.valueOf(i))));
String result = chapterNumber.substring(0, chapterNumber.indexOf(","));
This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .
This question already has answers here:
Make String first letter capital in java
(8 answers)
Closed 8 years ago.
I need to convert the first letter of a String into a Capital if it is not already one for part of a project of mine. Can anyone help me please?
Try using this,
String str= "haha";
str.replaceFirst("\\w", str.substring(0, 1).toUpperCase());
Try this
String s = "this is my string";
s.substring(0,1).toUpperCase();
In Java, this replaces every alpha-numeric word (plus underscores) so its first character is uppercase:
Matcher m = Pattern.compile("\\b([a-z])(\\w+)").matcher(str);
StringBuffer bfr = new StringBuffer();
while(m.find()) {
m.appendReplacement(bfr,
m.group(1).toUpperCase() + "$2");
}
m.appendTail(bfr);
It does not alter words that are already uppercased.
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.