I have a string 0023525631
but what I want is the string 23525631
-- without the leading zeroes. How do I do this?
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String. The ^0+(?! $)"; To remove the leading zeros from a string pass this as first parameter and “” as second parameter.
str. erase(0, min(str. find_first_not_of('0'), str. size()-1));
Use the lstrip() Function Along With List Comprehension to Remove Leading Zeros in a String in Python. The lstrip() can be utilized to remove the leading characters of the string if they exist. By default, a space is the leading character to remove in the string.
Approach: We use the StringBuffer class as Strings are immutable. Count leading zeros by iterating string using charAt(i) and checking for 0 at the “i” th indices. Use StringBuffer replace function to remove characters equal to the above count using replace() method.
Use Reguler Expression like this,
NSString *str =@"000034234247236000049327428900000";
NSRange range = [str rangeOfString:@"^0*" options:NSRegularExpressionSearch];
str= [str stringByReplacingCharactersInRange:range withString:@""];
NSLog(@"str %@",str);
O/P:-str 34234247236000049327428900000
You can convert string to integer and integer to string if you are number.
Or
Declarate pointer to this string and set this pointer to a start of your string, then iterate your string and add to this pointer number of zeros on the start of string. Then you have pointer to string who starting before zeros, you must use pointer to obitain string without zeros, if you use string you can get string with zeros.
Or
You can reverse string, and iterate it from reverse, if you get some char != '0'
you stop iterating else you rewriting this char to null. After it you can reverse string to get back your integer in string without leading zeros.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With