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(","));
Related
This question already has answers here:
How can I remove a substring from a given String?
(14 answers)
Closed 2 years ago.
String string = "yesnoyesnoyesnoyesno";
String substring = "no";
How do I remove every occurrence of the substring from the string so it ends up being "yesyesyesyes"?
If you are using Java 8+, then you could try splitting the input on "no" and then joining the resulting array together into a string:
String string = "yesnoyesnoyesnoyesno";
string = String.join("", string.split("no"));
System.out.println(string);
This prints:
yesyesyesyes
This doesn't use the replace(), so technically it meets the brief:
String str = string.replaceAll("no", "");
String input = "yesnoyesnoyesno";
String output="";
for(String token : input.split("no") )
output = output.concat(token);
System.out.println(output);
It prints:
yesyesyesyes
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.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
I have the code:
String str="Date-01-2017"
How can I print 2017 only from this string?
I don't know how to split and get the return as 2017.
You can try:
String[] splitString = dateString.split("-");
String year = splitString[2];
You can use substring method to take the part of the string you need:
String data = "Date-01-2017";
String year = data.substring(data.length() - 4, data.length());
You could use regular expression:
Pattern pattern = Pattern.compile("\\w+-(?<month>\\d{2})-(?<year>\\d{4})");
Matcher matcher = pattern.matcher("Date-01-2017");
if(matcher.matches())
System.out.println(matcher.group("year"));
else
System.out.println("NOT MATCH!!!");
demo
Another way to print if the string "2017" is using a function, such as this.
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
You can make a call to this function, with the string as the parameter, and if the string contains 2017, then u can print out that string.
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.