Appending NSString with Zeros depending upon length [duplicate] - ios

This question already has answers here:
objective-c right padding
(3 answers)
Closed 5 years ago.
I have a string, length may vary but I have to check if its length is less then 12 and if yes append those many zeros to it.
Like:
str = Test123, output should be Test12300000
str = Test123456, output should be Test12345600
Only 12 Char is allowed in string.
Tried below code but not getting generic result. There must be a better and easy way to do.
- (NSString *)formatValue:(NSString *)str forDigits:(NSString *)zeros {
return [NSString stringWithFormat:#"%#%#", str ,zeros];
}

how about something like this:
- (NSString *)stringToFormat:(NSString *)str {
while (str.length <12) {
str = [NSString stringWithFormat:#"%#0", str];
}
return str;
}

This is fast and simple, I think.
- (NSString *)stringToFormat:(NSString *)str {
return [str stringByAppendingString:[#"000000000000" substringFromIndex:str.length]];
}
Just make sure the string is never more than 12 characters.

Related

How can I extract the first 3 characters from an NSString? [duplicate]

This question already has answers here:
how to get first three characters of an NSString?
(3 answers)
Closed 6 years ago.
How can I extract the first 3 characters from a NSString?
For example if I have a string "1234820" How can I extract the numbers 123 from the string and store the result in a new string with the format "1.23 Million" ?
I am counting the the number of characters in the string by using
-(NSString *)returnFormattedString:(NSString *)stringToFormat{
NSString *formatedString;
NSUInteger characterCount = [stringToFormat length];
if (characterCount > 6) {
//???? How do I extract and add a decimal
stringWithThreeCharactersAndDecimal = ????;
NSString *string = [NSString stringWithString:stringWithThreeCharactersAndDecimal];
formatedString = [string stringByAppendingString:#"Million"];
}
return formatedString;
}
do like
assume that is your String
yourString = #"1234820";
// use substringToIndex for fetch First Three Character
yourString=[yourString substringToIndex:3];
// finally convert string to as like 1.23
NSString *finalStr = [NSString stringWithFormat:#"%.2f Million", [yourString floatValue]];
Choice -2
as per Droppys short and good answer is
NSString *finalStr = [NSString stringWithFormat:#"%.2f Million", [yourString floatValue] / 100.0f];

Extract integer from a string in objective c [duplicate]

This question already has answers here:
Objective-C: Find numbers in string
(7 answers)
Closed 7 years ago.
i have a string which has both numeric and character value.
Like: string = abc1234
Now I want to get only the integer part from it:
i.e. 1234
How can I do this? I have tried the following with no luck:
NSString *str = #"abc123";
int s = [str intValue];
Use this function
- (NSString *)extractNumberFromText:(NSString *)text
{
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:#""];
}
It will help you.Thankyou

Changing variable from int to string [duplicate]

This question already has answers here:
How to convert int to NSString?
(4 answers)
Closed 8 years ago.
Good morning,
First of all excuse me for my english.
A partner has made an app that place an image in the ios display depending the placement id you write on a textfield. He defined the textfield and the variable as a integer, but when he finished he realized that it should be an string. Now its all coded for a int, is there an easy way to parse it to string?
See this other SO question: How can I convert an int to an NSString?
In short, with the code NSString *string = [NSString stringWithFormat:#"%d", theinteger];
int yourIntVar = ??;
NSString *intIntoString = [NSString stringWithFormat:#"%d", yourIntVar];
use this:
myTextfield.text = [NSString stringWithFormat:#"%d",myInt];
int num = 2;
NSString *yourString = [NSString stringWithFormat:#"%d",num];

How to filter a string after a particular character in iOS? [duplicate]

This question already has answers here:
Split an NSString to access one particular piece
(7 answers)
Closed 9 years ago.
I want to filter string after character '='. For eg if 8+9=17 My output should be 17. I can filter character before '=' using NSScanner, how to do its reverse??? I need a efficient way to do this without using componentsSeparatedByString or creating an array
Everyone seems to like to use componentsSeparatedByString but it is quite inefficient when you just want one part of a string.
Try this:
NSString *str = #"8+9=17";
NSRange equalRange = [str rangeOfString:#"=" options:NSBackwardsSearch];
if (equalRange.location != NSNotFound) {
NSString *result = [str substringFromIndex:equalRange.location + equalRange.length];
NSLog(#"The result = %#", result);
} else {
NSLog(#"There is no = in the string");
}
Update:
Note - for this specific example, the difference in efficiencies is negligible if it is only being done once.
But in general, using componentsSeparatedByString: is going to scan the entire string looking for every occurrence of the delimiter. It then creates an array with all of the substrings. This is great when you need most of those substrings.
When you only need one part of a larger string, this is very wasteful. There is no need to scan the entire string. There is no need to create an array. There is no need to get all of the other substrings.
NSArray * array = [string componentsSeparatedByString:#"="];
if (array)
{
NSString * desiredString = (NSString *)[array lastObject]; //or whichever the index
}
else
{
NSLog(#""); //report error - = not found. Of array could somehow be not created.
}
NOTE:
Though this is very popular splitting solution, it is only worth trying whenever every substring separated by separator string is required. rmaddy's answer suggest better mechanism whenever the need is only to get small part of the string. Use that instead of this approach whenever only small part of the string is required.
Try to use this one
NSArray *arr = [string componentsSeparatedByString:#"="];
if (arr.count > 0)
{
NSString * firstString = [arr objectAtIndex:0];
NSString * secondString = [arr objectAtIndex:1];
NSLog(#"First String %#",firstString);
NSLog(#"Second String %#",secondString);
}
Output
First String 8+9
Second String 17
Use this:
NSString *a =#"4+6=10";
NSLog(#"%#",[a componentsSeparatedByString:#"="])
;
Log: Practice[7582:11303] (
"4+6",
10
)

Extracting from the right of a string in objective C [duplicate]

This question already has answers here:
Get last 2 characters of a string?
(2 answers)
Closed 9 years ago.
This seems to be what I'm looking for but in reverse. I would like the string to extract from the right not from the left.
The example extracting from the left is given:
NSString *source = #"0123456789";
NSString *firstFour = [source substringToIndex:4];
Output: "0123"
I'm looking for a version of the below that works from the right (what is below doesn't work)
NSString *source = #"0123456789";
NSString *lastFour = [source substringToIndex:-4];
Output: "6789"
the [source substringFromIndex:6]; won't work because sometimes I will get an answer that is 000123456789 or 456789 or 6789. In all cases I just need the last 4 characters from the string so that I can convert it to a number.
there must be a better way than a bunch of if else statements?
As you are not sure, about the length of the string, so you must check it before extracting like this:
NSString *source = #"0123456789";
NSNumber *number;
if (source.length>=4) {
NSString *lastFour=[source substringFromIndex:source.length-4];
number=#([lastFour integerValue]); //and save it in a number, it can be int or NSInteger as per your need
}
NSLog(#"%#",number);
Also if you want a quick method that you need to call several times, create a category :
#implementation NSString (SubstringFromRight)
-(NSString *)substringFromRight:(NSUInteger)from{
if (self.length<from) {
return nil;
}
return [self substringFromIndex:self.length-from];
}
#end
And use it as :NSLog(#"%#",[source1 substringFromRight:4]);
NSString *source = #"0123456789";
NSString *newString = [source substringFromIndex:[source length] - 4];
NSLog(#"%#",newString);
replace
NSString *lastFour = [source substringToIndex:-4];
with
NSString *lastFour = [source substringFromIndex:[source length] - 4];
which returns you the last 4 characters of your original string string in lastFour string.
You can use the following code to get last 4 characters from your string.
NSString *last4Characters = [source substringFromIndex:(source.length - 4)];
NSLog(#"Last 4 Characters:%#",last4Characters);
last4Characters=nil;
Please let me know if any issue.

Resources