i have this code:
NSString * firstdDigitTest = [numberString substringToIndex:1];
if (! [firstdDigitTest isEqualToString:#"0"])
{
numberString = [numberString substringFromIndex:1];
self.firstImageName = [namefile stringByAppendingString:firstdDigitTest];
}
self.number = [numberString integerValue];
i check if the first number is not 0 and if it's not 0 i need to insert it to the firstImageName. But when i do it (using the substringFromIndex) and i try to use integerValue it's dosent work!
without the substringFromIndex it's works! :(
NSString *str0 = #"XXX10098";
NSString *str1 = [str0 substringWithRange:NSMakeRange(4, str0.length-4)];
NSLog(#"%#", str1);
NSLog(#"%d", [str1 intValue]);
2012-10-28 10:52:07.309 iFoto[5652:907] 0098
2012-10-28 10:52:07.312 iFoto[5652:907] 98
check [numberString intValue];
Related
I want to remove all occurrence of 0 pairs before appearing any digit(1-9) from NSString
if 000001234000 required 01234000
if 0000123400 required 123400
if 012340000 required 012340000
if 00000012 required 12
Can anyone help ? thanks.
Perhaps not the most elegant solution (but only trims leading '00'):
- (NSString *)trimLeadingDoubleZerosFrom:(NSString *)str {
if (str.length > 1 ) {
if ([[str substringWithRange:NSMakeRange(0, 2)] isEqualToString:#"00"]) {
return [self trimLeadingDoubleZerosFrom:[str substringFromIndex:2]];
}
}
return str;
}
Seems to work for your examples:
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"000001234000"]);// returns 01234000
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"0000123400"]); // returns 123400
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"012340000"]); // returns 012340000
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"00000012"]); // returns 12
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"12"]); // returns 12
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"02"]); // returns 02
NSLog(#"%#", [self trimLeadingDoubleZerosFrom:#"2"]); // returns 2
NSString *MyString = #"00000123050060007";
NSString *NewString = [self RemovePairOfZero:MyString];
NSLog(#"OUTPUT:: %#", NewString);
- (NSString *)RemovePairOfZero:(NSString *)Param
{
if ([Param length] > 1 )
{
if ([[Param substringWithRange:NSMakeRange(0, 2)] isEqualToString:#"00"])
{
return [self RemovePairOfZero:[Param substringFromIndex:2]];
}
}
return Param;
}
//2013-09-24 13:15:58.527 Test[1584:907] OUTPUT:: 0123050060007
NSString* a = #"00000123";
while ([a hasPrefix:#"00"]) {
a = [a stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:#""];
}
You can try using stringByreplacingOccurancesOfString:#"00" withString:#""
NSString *s= #"00000123";
NSLog(#"%#",s);
s = [s stringByReplacingOccurrencesOfString:#"00" withString:#"."];
NSLog(#"%#",s);
Try this one..
That can be done by simply replacing #"00" with #""
NSString *string = #"00000123";
string = [string stringByReplacingOccurrencesOfString:#"00" withString:#""];
Hope that helps!
Code Snippet:
NSString *tempStr = self.consumerNumber.text;
if ([tempStr hasPrefix:#"0"] && [tempStr length] > 1) {
tempStr = [tempStr substringFromIndex:1];
[self.consumerNumbers addObject:tempStr];>
}
I tried those things and removing only one zero. how to remove more then one zero
Output :001600240321
Expected result :1600240321
Any help really appreciated
Thanks in advance !!!!!
Try to use this one
NSString *stringWithZeroes = #"001600240321";
NSString *cleanedString = [stringWithZeroes stringByReplacingOccurrencesOfString:#"^0+" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, stringWithZeroes.length)];
NSLog(#"Clean String %#",cleanedString);
Clean String 1600240321
convert string to int value and re-assign that value to string,
NSString *cleanString = [NSString stringWithFormat:#"%d", [string intValue]];
o/p:-1600240321
You can add a recursive function that is called until the string begin by something else than a 0 :
-(NSString*)removeZerosFromString:(NSString *)anyString
{
if ([anyString hasPrefix:#"0"] && [anyString length] > 1)
{
return [self removeZerosFromString:[anyString substringFromIndex:1]];
}
else
return anyString;
}
so you just call in your case :
NSString *tempStr = [self removeZerosFromString:#"000903123981000"];
NSString *str = #"001600240321";
NSString *newStr = [#([str integerValue]) stringValue];
If the NSString contains numbers only.
Other wise use this:
-(NSString *)stringByRemovingStartingZeros:(NSString *)string
{
NSString *newString = string;
NSInteger count = 0;
for(int i=0; i<[string length]; i++)
{
if([[NSString stringWithFormat:#"%c",[string characterAtIndex:i]] isEqualToString:#"0"])
{
newString = [newString stringByReplacingCharactersInRange:NSMakeRange(i-count, 1) withString:#""];
count++;
}
else
{
break;
}
}
return newString;
}
Simply call this method:-
NSString *stringWithZeroes = #"0000000016909tthghfghf";
NSLog(#"%#", [self stringByRemovingStartingZeros:stringWithZeroes]);
OutPut: 16909tthghfghf
Try the `stringByReplacingOccurrencesOfString´ methode like this:
NSString *new = [old stringByReplacingOccurrencesOfString: #"0" withString:#""];
SORRY: This doesn't help you due to more "0" in the middle part of your string!
how can I display 5 raise to 1/3 in iphone i.e I want 1/3 written above 5 can anyone help please
I Found this solution, hope so it would be helpful for you.
x to the power of y in a UILabel could be easy. Just replace your indices with unicode superscript characters... I use the following method to turn an integer into a string with superscript characters.
+(NSString *)convertIntToSuperscript:(int)i
{
NSArray *array = [[NSArray alloc] initWithObjects:#"⁰", #"¹", #"²", #"³", #"⁴", #"⁵", #"⁶", #"⁷", #"⁸", #"⁹", nil];
if (i >= 0 && i <= 9) {
NSString *myString = [NSString stringWithFormat:#"%#", [array objectAtIndex:i]];
[array release];
return myString;
}
else {
NSString *base = [NSString stringWithFormat:#"%i", i];
NSMutableString *newString = [[NSMutableString alloc] init];
for (int b = 0; b<[base length]; b++) {
int temp = [[base substringWithRange:NSMakeRange(b, 1)] intValue];
[newString appendString:[array objectAtIndex:temp]];
}
[array release];
NSString *returnString = [NSString stringWithString:newString];
[newString release];
return returnString;
}
}
Try this NSString *cmsquare=#"cm\u00B2";
It will display cm².
Yes you can do that but you need custom UILabel, either Make it by yourself or Get it Open Source..
I need to convert a hex string to binary form in objective-c, Could someone please guide me?
For example if i have a hex string 7fefff78, i want to convert it to 1111111111011111111111101111000?
BR,
Suppi
Nice recursive solution...
NSString *hex = #"49cf3e";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:#"%#", [self toBinary:hexAsInt]];
-(NSString *)toBinary:(NSUInteger)input
{
if (input == 1 || input == 0)
return [NSString stringWithFormat:#"%u", input];
return [NSString stringWithFormat:#"%#%u", [self toBinary:input / 2], input % 2];
}
Simply convert each digit one by one: 0 -> 0000, 7 -> 0111, F -> 1111, etc. A little lookup table could make this very concise.
The beauty of number bases that are powers of another base :-)
In case you need leading zeros, for example 18 returns 00011000 instead of 11000
-(NSString *)toBinary:(NSUInteger)input strLength:(int)length{
if (input == 1 || input == 0){
NSString *str=[NSString stringWithFormat:#"%u", input];
return str;
}
else {
NSString *str=[NSString stringWithFormat:#"%#%u", [self toBinary:input / 2 strLength:0], input % 2];
if(length>0){
int reqInt = length * 4;
for(int i= [str length];i < reqInt;i++){
str=[NSString stringWithFormat:#"%#%#",#"0",str];
}
}
return str;
}
}
NSString *hex = #"58";
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:#"%#", [self toBinary:hexAsInt strLength:[hex length]]];
NSLog(#"binario %#",binary);
I agree with kerrek SB's answer and tried this.
Its work for me.
+(NSString *)convertBinaryToHex:(NSString *) strBinary
{
NSString *strResult = #"";
NSDictionary *dictBinToHax = [[NSDictionary alloc] initWithObjectsAndKeys:
#"0",#"0000",
#"1",#"0001",
#"2",#"0010",
#"3",#"0011",
#"4",#"0100",
#"5",#"0101",
#"6",#"0110",
#"7",#"0111",
#"8",#"1000",
#"9",#"1001",
#"A",#"1010",
#"B",#"1011",
#"C",#"1100",
#"D",#"1101",
#"E",#"1110",
#"F",#"1111", nil];
for (int i = 0;i < [strBinary length]; i+=4)
{
NSString *strBinaryKey = [strBinary substringWithRange: NSMakeRange(i, 4)];
strResult = [NSString stringWithFormat:#"%#%#",strResult,[dictBinToHax valueForKey:strBinaryKey]];
}
return strResult;
}
my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];