Reputation: 137
I'm trying to remove leading zeros from a string where there are multiple other strings and numbers in java
so ow - 00250 = 00000000 ]
for the above input I'm trying to get the output to be
so ow - 250 = 0 ]
I have tried
removeLeadingZeros(str.trim().replaceAll(" +", " ")
where removeLeadingZeros is
// Regex to remove leading
// zeros from a string
String regex = "^0+(?!$)";
// Replaces the matched
// value with given string
str = str.replaceAll(regex, "");
return str;
but I am getting the output
so ow - 00250 = 00000000 ]
which is the same as the original input.
These methods seem to only work for the first element in the string if it is a number such as
00015039 so ow + - 003948 83
which returns
15039 so ow + - 003948 83
but want to return
15039 so ow + - 3948 83
thanks!
Upvotes: 1
Views: 1599
Reputation: 1629
You can split the string at each space character, then process each substring through your function, and then join them together again.
String[] substrings = myString.split(" ", 0);
for (int i = 0; i < substring.length; i++) {
substrings[i] = removeLeadingZeros(substrings[i]);
}
myString = String.Join(" ", substrings);
This code is untested. I'm writing this from my phone, so I haven't been able to put it through its paces. Hopefully the general idea comes through.
Upvotes: 0
Reputation: 626738
You can use
String result = text.replaceAll("\\b(?<!\\d[,.])(?:(0)+\\b|0+(?=[1-9]))", "$1").replaceAll("(?U)\\s+", " ").trim();
See the regex demo.
Depending on how messy your input is you might need to adjust or even expand the solution. Details:
\b(?<!\d[,.])
- a word boundary, there must be start of string or a non-word char immediately to the right, and there can be no digit + a dot or a comma immediately to the left of the current location (that is, we exclude the decimal digit matching)(?:(0)+\b|0+(?=[1-9]))
- either of the two patterns:
(0)+\b
- 0
, one or more times, the last 0
is saved in Group 1, followed with a word boundary|
- or0+(?=[1-9])
- one or more 0
chars followed with a non-zero digit..replaceAll("(?U)\\s+", " ")
shrinks any Unicode whitespace character chunks into a single ASCII space.
Upvotes: 0
Reputation: 338326
Arrays
.stream( input.split( " " ) )
.filter( s -> ! s.isBlank() )
.map(
s -> {
try{
return String.valueOf( Integer.parseInt( s ) ) ;
} catch ( NumberFormatException e ) {
return s ;
}
}
)
.collect( Collectors.joining( " " ) )
int
I would split the string into parts, delimiting by SPACE character.
input.split( " " )
Splitting results in an array. Make a stream of that array.
String input = "so ow - 00250 = 00000000 ]" ;
String output =
Arrays
.stream( input.split( " " ) )
…
All those extra spaces will result in a bunch of chunks that are empty strings, no text within. So filter those out.
And we want to eliminate whitespace as well as empty strings, so call String#isBlank
rather than String#isEmpty
.
.filter( s -> ! s.isBlank() )
Try to parse each part as an integer number. If the parsing fails, an exception is thrown. Trap for that exception. If caught, simply return the text that failed to parse. If no exception, return a string of the newly minted int
value.
.map(
s -> {
try{
int x = Integer.parseInt( s ) ;
return String.valueOf( x ) ;
} catch ( NumberFormatException e ) {
return s ; // Dismiss the exception.
}
}
)
Collect all our remaining and newly-created parts with a SPACE as their delimiter.
.collect( Collectors.joining( " " ) )
Pulling all that code together looks like this:
String input = "so ow - 00250 = 00000000 ]" ;
String output =
Arrays
.stream( input.split( " " ) )
.filter( s -> ! s.isBlank() )
.map(
s -> {
try{
int x = Integer.parseInt( s ) ;
return String.valueOf( x ) ;
} catch ( NumberFormatException e ) {
return s ; // Swallow the exception.
}
}
)
.collect( Collectors.joining( " " ) )
;
System.out.println( output ) ;
See that code run live at IdeOne.com.
so ow - 250 = 0 ]
Upvotes: 1
Reputation: 82
By splitting the given string into array and then remove the leading zeroes, is working fine.
where removeLeadingZeros is:
public static String removeLeading(String str) {
String regex = "^0+(?!$)";
String x[] = str.split(" ");
StringBuilder z = new StringBuilder();
for(String y:x) {
str = y.replaceAll(regex, "");
z.append(" " + str);
}
return z.toString();
}
Upvotes: 2