i have a NSString object named as myString.i need to append a space in each side of my string.
can any one tell me the good way to do it?
myString = [NSString stringWithFormat:#" %# ", myString];
NSString *myNewString = [NSString stringWithFormat: #" %# ", myString];
Then you can do it in this way.
NSString *s = [[NSString alloc]initWithFormat:#" %# ",str];
Related
I have a price parameter from my API price: 10
I want to show it in my textfield with appending "$" after it. But it doesn't show my number anyway even with casting it. Here is my code :
cell.price.text = [[NSString stringWithFormat:#"%i",
(NSNumber*)DIC[#"items"][indexPath.row][#"price"]] stringByAppendingString:#" $"];
to print an NSNumber you need to use %# instead of %i
cell.price.text = [NSString stringWithFormat:#"%# $",
(NSNumber*)DIC[#"items"][indexPath.row][#"price"]];
NSNumber is an object, do using the format %i will not work.
cell.price.text = [NSString stringWithFormat:#"%# $",
(NSNumber*)DIC[#"items"][indexPath.row][#"price"]];
I need to split NSString to array by specific word.
I've tried to use [componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]]
But the split is performed by a single character, and I need a few chars.
Example:
NSString #"Hello great world";
Split key == #" great ";
result:
array[0] == #"Hello";
array[1] == #"world";
Try
NSString *str = #"Hello great world";
//you can use the bellow line to remove space
//str = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
// split key = #"great"
NSArray *arr = [str componentsSeparatedByString:#"great"];
Code:
NSString *string = #"Hello great world";
NSArray *stringArray = [string componentsSeparatedByString: #" great "];
NSLog(#"Array 0: %#" [stringArray objectAtIndex:0]);
NSLog(#"Array 1: %#" [stringArray objectAtIndex:1]);
The easiest way is the following:
NSString *string = #"Hello Great World";
NSArray *stringArray = [string componentsSeparatedByString: #" "];
This can help you.
I would like to be able to right align a string using spaces. I have to be able to use the stringWithFormat: method.
So far I have tried the recommended format and it does not seem to work: [NSString stringWithFormat:#"%10#",#"test"].
I would expect this to return a string that has six spaces followed by "test" but all I am getting is "test" with no spaces.
It appears that stringWithFormat ignores the sizing requests of the %# format specifier. However, %s specifier works correctly:
NSString *test = #"test";
NSString *str = [NSString stringWithFormat:#"%10s", [test cStringUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"'%#'", str);
This prints ' test'.
It's C style formatting. %nd means the width is n.
check following code.
NSLog(#"%10#",[NSString stringWithFormat:#"%10#",#"test"]);
NSLog(#"%#",[NSString stringWithFormat:#" %#",#"test"]);
NSLog(#"%10#", #"test");
NSLog(#"%10s", [#"test" cStringUsingEncoding:[NSString defaultCStringEncoding]]);
NSLog(#"%10d", 1);
NSString *str = #"test";
int padding = 10-[str length]; //6
if (padding > 0)
{
NSString *pad = [[NSString string] stringByPaddingToLength:padding withString:#" " startingAtIndex:0];
str = [pad stringByAppendingString:str];
}
NSLog(#"%#", str);
I am working on iPhone app. I have the SQL query:
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:#"INSERT INTO colour VALUES("+myRed+")"];
It produces syntax error. How to insert a string into string. That approach in Java would have worked.
Best regards
Try this :
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(\"%#\")",myRed]];
If you don't want " then :-
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(%#)",myRed]];
Hope it helps you.
You could try writing your query and your string appended together before hand.
NSString *str = #"Your values";
NSString *query = [NSString stringWithFormat:#"INSERT INTO color VALUES(%#)", str];
[self insertData:query];
I would like to change it to first name and last initial.
Thanks!
NSString* nameStr = #"Firstname Lastname";
NSArray* firstLastStrings = [nameStr componentsSeparatedByString:#" "];
NSString* firstName = [firstLastStrings objectAtIndex:0];
NSString* lastName = [firstLastStrings objectAtIndex:1];
char lastInitialChar = [lastName characterAtIndex:0];
NSString* newNameStr = [NSString stringWithFormat:#"%# %c.", firstName, lastInitialChar];
This could be much more concise, but I wanted clarity for the OP :) Hence all the interim variables and var names.
This would do it:
NSArray *components = [fullname componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstnameAndLastnameInitial = [NSString stringWithFormat:#"%# %#.", [components objectAtIndex:0], [[components objectAtIndex:1] substringToIndex:1]];
This assumes that fullname is an instance of NSString and contains two components separated by whitespace, so you will need to check for that as well.
You can use this code snippet, first separate string using componentsSeparatedByString, then join them again but only get the first character of Lastname
NSString *str = #"Firstname Lastname";
NSArray *arr = [str componentsSeparatedByString:#" "];
NSString *newString = [NSString stringWithFormat:#"%# %#.", [arr objectAtIndex:0], [[arr objectAtIndex:1] substringToIndex:1]];
Get an array of the parts of the name individually:
NSString *sourceName = ...whatever...;
NSArray *nameComponents =
[sourceName
componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
Then, I guess:
NSString *compactName =
[NSString stringWithFormat:#"%# %#.",
[nameComponents objectAtIndex:0],
[[nameComponents lastObject] substringToIndex:1]];
That'll skip any middle names, though if there's only one name, like say 'Jeffry' then it'll output 'Jeffry J.'. If you pass in the empty string then it'll raise an exception when you attempt to get objectAtIndex:0 since that array will be empty. So you should check [nameComponents count].