How to right pad a string using stringWithFormat - ios

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);

Related

In objective-c how to get characters after n-th?

I have a number which will be represented as string. It is longer than 4 chars. I need to create new string from 5th till the end for that number.
For example if I have 56789623, I need to have 9623 as a result (5678 | 9623).
How to do that?
P.S. I suppose that this is very simple question, but I don't know how properly ask Google about that.
NSString *str = #"56789623";
NSString *first, *second;
if ([str length] > 4) {
first = [str substringWithRange:NSMakeRange(0, 4)];
second = [str substringWithRange:NSMakeRange(4, [str length] - 4)];
} else {
first = str;
second = nil;
}
Use this Simple functions
- (NSString *)substringFromIndex:(NSUInteger)from;
- (NSString *)substringToIndex:(NSUInteger)to;
- (NSString *)substringWithRange:(NSRange)range;
You can use:
- (NSString *)substringFromIndex:(NSUInteger)anIndex
NSString *number = #"56789623";
NSString *result = [number substringFromIndex:4];
NSLog(#"%#", result);
result contains the string: #"9623"
The keywords you were looking for are: substring and range. There are several ways to use them. Example code split string into 2 equal (if number of characters is even almost equal) substrings:
NSString *str = #"56789623";
NSInteger middleIndex = (NSInteger)(str.length/2);
NSString *strFirstPart = [str substringToIndex:middleIndex];
NSString *strSecondPart = [str substringFromIndex:middleIndex];
NSString *strFirstPart2 = [str substringWithRange:NSMakeRange(0, middleIndex)];
NSString *strSecondPart2 = [str substringWithRange:NSMakeRange(middleIndex, [str length]-middleIndex)];

Special Character to Original string - Objective C

I have tried the string(contain special character) to original String. The string received from service. I tried many codes. But i cant get the original String.
i tried the following code
// \U00e0\U00ae\U0089\U00e0\U00ae\U00b2\U00e0\U00ae\U0095\U00e0\U00ae\U00ae\U00e0\U00af\U008d
NSString *name2escaped = #"\\U00e0\\U00ae\\U0089\\U00e0\\U00ae\\U00b2\\U00e0\\U00ae\\U0095\\U00e0\\U00ae\\U00ae\\U00e0\\U00af\\U008d";
NSString *name2 = [NSString stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"name2 = %#", name2);
inputvalue.stringValue = name2;
NSLog (#"%#",name2);
Output prints: à®à®²à®à®®à¯
I worked in Mac OS X development 10.9 Mavericks
Service sometimes send chinese character also. Can anybody help me
As according to your requirement output should be Tamil or Chinese or English Language
Change NSNonLossyASCIIStringEncoding to NSUTF16StringEncoding :
NSString *name2escaped = #"\\U00e0\\U00ae\\U0089\\U00e0\\U00ae\\U00b2\\U00e0\\U00ae\\U0095\\U00e0\\U00ae\\U00ae\\U00e0\\U00af\\U008d";
NSString *name2 = [NSString stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF16StringEncoding];
NSLog(#"name2 = %#", name2);
Output is : name2 = 屵〰攰屵〰慥屵〰㠹屵〰攰屵〰慥屵〰戲屵〰攰屵〰慥屵〰㤵屵〰攰屵〰慥屵〰慥屵〰攰屵〰慦屵〰㡤 –
I did it
My code is
- (NSString *) OrigninalString:(NSString *)getfilername
{
const char *c;
NSString *name2escaped = [NSString stringWithFormat:#"%#",getfilername];
NSString *string = [NSString
stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
c = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
if (!c) {
c = [name2escaped cStringUsingEncoding:NSISOLatin1StringEncoding];
}
NSString *newString = [NSString stringWithCString:c encoding:NSUTF8StringEncoding];
c = nil;
string = nil;
return newString;
}

How to remove charactor '\' in NSString?

How to remove character '\' in NSString IOS
For example,
NSString *abc = #"bmcvn\nmsf;
I have tried:
NSString *stri = #"\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:#"\\" withString:#"123"];
NSLog(str);
but it didn't replace
Note the difference between replacing the backslash char
NSString *original = #"foo\\bar";
NSLog(#"%#", original); // Prints: foo\bar
NSString *replaced = [original stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSLog(#"%#", replaced); // Prints: foobar
And replacing a char that is represented using a backslash (\r, \n, \t, ...):
NSString *original = #"thisIsCarriageReturn\rRightThere";
NSLog(#"%#", original); // Prints: thisIsCarriageReturn
// RightThere
NSString *replaced = [original stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSLog(#"%#", replaced); // Prints: thisIsCarriageReturnRightThere
You can't replace "\" in the string "\rdffsdf" because it is part the two character representation "\r" of the single character with the hex value 0x0d (13 decimal). It is the "carriage return" character.
When a carriage return character (0x0d) is needed in a string it is entered as "\r". Also common are the two character sequences line feed "\n" (0X0a), horizontal tab "\t" (0X09) and finally backslash "\" (0x5c).
The backslash case:
NSString *stri = #"\\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:#"\\" withString:#"123"];
NSLog(#"str: %#", str);
NSLog output:
str: 123rdffsdf
NSString *s = #"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"%#", s); // => foobarbazfoo
Replace multiple characters in a string in Objective-C?
Since the back slash character is an escape character you have to write two of them.
- (NSString *)sanitizeString:(NSString *)string {
NSCharacterSet* illegalCharacters = [NSCharacterSet characterSetWithCharactersInString:#"\n"];
return [[string componentsSeparatedByCharactersInSet: illegalCharacters] componentsJoinedByString:#"n"];
}

How to get a character before last letter in NSString?

I have NSString with input Value from keyboard.
Eg.
NSString *myText = #"Apple";
In my case , i want to get a word before last letter.
For above eg , i want to get only l letter before e letter.
How can i get it?
NSString *text = #"Apple";
unichar c = [text characterAtIndex:text.length - 2];
If you need a NSString
NSString *character = [NSString stringWithCharacters:&c length:1];
that may be a useful implenentaion as well:
NSString *_string = #"string";
NSString *_letter = nil;
if (_string.length > 1) {
[_string substringWithRange:NSMakeRange(_string.length - 2, 1)];
}
it does not crash either, when the string is not long enough.

iOS: changing NSString value

Will this bit of code produce any memory leaks? Is it the correct way to change NSString values?
NSString * enemiesAndElementsTextureFileName = #"bla bla";
enemiesAndElementsTextureFileName = #"bl";
That way of doing it won't cause any memory leaks and it is indeed correct. In this case you wouldn't need an NSMutableString because you aren't altering the string literal itself, you are simply replacing the string value with a new one (replacing #"bla bla" with #"bl").
In this case, however, your string will now be 'bl', so you can delete that first line value and just have NSString * enemiesAndElementsTextureFileName = #"bl";
Yes NSString allocated once. This is one of the way
Yes, use NSMutableString with the following method as your needs:
// Allocate
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
// set string content
[str setString:#"1234"];
// Append
[str appendString:#"567"];
// Concat
[str appendFormat:#"age is %i and height is %.2f", 27, 1.55f];
// Replace
NSRange range = [str rangeOfString:#"height"];//查找字符串height的位置
[str replaceCharactersInRange:range withString:#"no"];
// Insert
[str insertString:#"abc" atIndex:2];
// Delete
range = [str rangeOfString:#"age"];
[str deleteCharactersInRange:range];
NSLog(#"%#", str);

Resources