Separate NSMutablestring with comma [duplicate] - ios

This question already has answers here:
How to append a NSMutableString with a NSMutableString?
(4 answers)
Closed 9 years ago.
i'm trying to append two NSMutablestring, but i want to make the comma separation between them. Right now the code i use is this:
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
NSLog(#"%#",teststring);
the result is :This is a stringThis is a String
i try with:
[teststring appendFormat:#",%#",astring];
but still no luck.
Please help, regards.

teststring = [[NSMutableString alloc]init];
[teststring appendString:#","];
[teststring appendString:astring];

Try this
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
[teststring appendString:#","];
[teststring appendString:astring];
NSLog(#"%#",teststring);

Try this
NSString *string = #"Hello World";
NSMutableString * secondstring =[[NSMutableString alloc]init];
[secondstring appendString:string]
[secondstring appendString:#","];
[secondstring appendString:string];
If you want to see this String
NSLog(#"%#",secondstring);

I used this and its working fine for me :
NSMutableString *teststring = [[NSMutableString alloc]init];
NSMutableString *astring = [[NSMutableString alloc]init];
[astring appendString:#"I am appending this string"];
[teststring appendFormat:#",%#",astring];
NSLog(#"string : %#",teststring);
O/P : string : ,I am appending this string

Related

Split NSString to array by specific word

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.

Assigning charAtIndex to stringWithCharacters gives invalid cast warning and bad access error

I'm trying to grab firstname and lastname from firstname+lastname.
int loop=0;
NSMutableString *firstname = [[NSMutableString alloc]init];
NSMutableString *fullName = [[NSMutableString alloc]initWithString:#"Anahita+Havewala"];
for (loop = 0; ([fullName characterAtIndex:loop]!='+'); loop++) {
[firstname appendString:[NSString stringWithCharacters:(const unichar *)[fullName characterAtIndex:loop] length:1]];
}
NSLog(#"%#",firstname);
I tried typecasting from unichar to const unichar* because characterAtIndex returns a unichar but stringWithCharacters accepts a const unichar.
This causes a cast from smaller integer type warning and the app crashes (bad access) when this line is encountered.
Why are string operations so complicated in Objective C?
You can easily get first name and last using componentsSeparatedByString: method.
NSMutableString *fullName = [[NSMutableString alloc]initWithString:#"Anahita+Havewala"];
NSArray *components = [fullName componentsSeparatedByString:#"+"];
NSString *firstName = components[0];
NSString *lastName = components[1];
Note: You need to do proper array bounds check. Also you can use NSScanner for the same purpose.
Try this out:
NSMutableString *firstname = [[NSMutableString alloc] init];
NSMutableString *fullName = [[NSMutableString alloc] initWithString:#"Anahita+Havewala"];
for (NSUInteger loop = 0; ([fullName characterAtIndex:loop]!='+'); loop++) {
unichar myChar = [fullName characterAtIndex:loop];
[firstname appendString:[NSString stringWithFormat:#"%C", myChar]];
}
NSLog(#"%#", firstname);

how to convert NSString to NSArray [duplicate]

This question already has answers here:
Comma-separated string to NSArray in Objective-C
(2 answers)
Closed 8 years ago.
I have a string like
NSString* str = #"[90, 5, 6]";
I need to convert it to an array like
NSArray * numbers = [90, 5 , 6];
I did a quite long way like this:
+ (NSArray*) stringToArray:(NSString*)str
{
NSString *sep = #"[,";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *temp=[str componentsSeparatedByCharactersInSet:set];
NSMutableArray* numbers = [[NSMutableArray alloc] init];
for (NSString* s in temp) {
NSNumber *n = [NSNumber numberWithInteger:[s integerValue]];
[numbers addObject:n];
}
return numbers;
}
Is there any neat and quick way to do such conversion?
Thanks
First remove the unwanted characters from the string, like white spaces and braces:
NSString* str = #"[90, 5, 6]";
NSCharacterSet* characterSet = [[NSCharacterSet
characterSetWithCharactersInString:#"0123456789,"] invertedSet];
NSString* newString = [[str componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:#""];
You will have a string like this: 90,5,6. Then simply split using the comma and convert to NSNumber:
NSArray* arrayOfStrings = [newString componentsSeparatedByString:#","];
NSMutableArray* arrayOfNumbers = [NSMutableArray arrayWithCapacity:arrayOfStrings.count];
for (NSString* string in arrayOfStrings) {
[arrayOfNumbers addObject:[NSDecimalNumber decimalNumberWithString:string]];
}
Using the NSString category from this response it can be simplified to:
NSArray* arrayOfStrings = [newString componentsSeparatedByString:#","];
NSArray* arrayOfNumbers = [arrayOfStrings valueForKey: #"decimalNumberValue"];
NSString* str = #"[90, 5, 6]";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:#"[] "];
NSArray *array = [[[str componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:#""]
componentsSeparatedByString:#","];
Try like this
NSArray *arr = [string componentsSeparatedByString:#","];
NSString *newSTR = [str stringByReplacingOccurrencesOfString:#"[" withString:#""];
newSTR = [newSTR stringByReplacingOccurrencesOfString:#"]" withString:#""];
NSArray *items = [newSTR componentsSeparatedByString:#","];
You can achieve that using regular expression 
([0-9]+)
NSError* error = nil;
NSString* str = #"[90, 5, 6]";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:#"([0-9]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
Then you have a NSArray of string, you just need to iterate it and convert the strings to number and insert them into an array.

IOS NSString get characters before '#"

for example i have string like this:
NSString *one = B3#This is the first string
NSString *two = 1#This is the second string
How can i get the "B3" and "1" Character only (using objective C)
Thanks..
Turns out this is one way to do it:
NSRange range = [one rangeOfString:#"#" options:NSBackwardsSearch];
NSString *newString = [one substringToIndex:range.location];
Thanks for all the answers.
NSString* one = #"B3#";
NSString* two = #"1#";
NSString* result = [one stringByReplacingOccurrencesOfString:#"#" withString:#""];
NSString* result_2 = [two stringByReplacingOccurrencesOfString:#"#" withString:#""];
//if you need to marge
NSString* tot = [NSString stringWithFormat:#"%#%#",result,result_2];

A better approach to coding the a string value test for a NSDictionary object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How could I write this if else code checking in a better way?
My conditional code here seems repetitive and long. Is there a better approach? I want to test for a string value in a NSDictionary object and then depending upon the value prefix a UILabel with $, £, ¥ currency symbols.
Here's my code (I've just shown 2 examples below, I have more currencies and the code is very long):
if ([[item objectForKey:#"currency"] isEqualToString:#"EUR"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"GBP"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"USD"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"$%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
thanks so much for any help.
You can refactor your code to this (possible because essentially 80% of the code within the if statements are identical):
NSDictionary *currDict = #{
#"EUR": #"€",
#"GBP": #"₤",
#"USD": #"$"
};
NSString *currName = [item objectForKey:#"currency"];
NSString *currency = [currDict objectForKey:currName];
NSString *priceConvertToStr = [NSString stringWithFormat:#"%#%#",
currency,
[[item objectForKey:#"price"] stringValue]
];
NSString *priceStringFix = [priceConvertToStr stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];

Resources