How to add multiple strings in NSMutableString separated by comma - ios

I want to create a comma separated string inside loop like this :-
string=#"A,B,C,D,E";
I did something like this :
for (int j=0; j<_arrayToCarryDataFromCartToPaymentPageInPaypal.count; j++)
{
NSMutableDictionary *paypalArrayDataInDictionary=[_arrayToCarryDataFromCartToPaymentPageInPaypal objectAtIndex:j];
[productNameDetail appendString:[NSString stringWithFormat:#"%#,", [paypalArrayDataInDictionary objectForKey:#"PRODUCT"]]];
}
Here _arrayToCarryDataFromCartToPaymentPageInPaypal is my MutableArray getting from plist.

Get array of PRODUCT from the _arrayToCarryDataFromCartToPaymentPageInPaypal and then use componentsJoinedByString on it to get the string separated by ,.
NSArray *products = [_arrayToCarryDataFromCartToPaymentPageInPaypal valueForKey:#"PRODUCT"];
NSString *str = [products componentsJoinedByString:#","];

Related

How can I change substring(s) in an array of strings in Objective-C?

I have a weird error with decoding & sign so its always shown as &, so && will be &&, a&b is shown as a&b etc.
I have those weird characters in my array of strings sometimes, so I want to remove them. To be more clear, if I have an array like this:
"str1", "a&b", "12345", "d&&emo&"
I want to it to be:
"str1", "a&b", "12345", "d&&emo&"
So all & should be changed into just &.
This is the code I tried, but it didn't help. Elements are not changing their values:
for (int i = 0; i <= self.array-1; i++)
{
NSLog(#"%#", self.array[i]);
[self.array[i] stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
NSLog(#"%#", self.array[i]);
}
I am new to Objective-C.
NSString instances are immutable, which means you can't change them once they are created. The stringByReplacingOccurencesOfString:withString: method actually returns a new NSString instance. You could use NSMutableString, or alternatively, you can create a new array containing the replacement strings, e.g.:
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *input in self.array)
{
NSString *replacement = [input stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
[newArray addObject:replacement];
}
self.array = newArray;

IOS:Objective-C: How to combine elements of arrays

I have an array of people objects stored in Core Data and I would like to create a string made by combining two attributes of each, as in firstname and lastname.
NSArray *firstArr = [[Employees valueForKey:#"first"] allObjects];
NSArray *secondArr = [[Employees valueForKey:#"last"] allObjects];
I can create a string from one attribute such as "John,Jane" with
NSString *firststr = [firstarr componentsJoinedByString:#","];
Can anyone suggest how to make a string that has the first and last names separated by commas as in John Doe, Jane Doe? Imagine there is a method but I cannot find it.
You can do it like this
NSArray *firstArr = #[#"Rajat" , #"Rajat1", #"Raj"];
NSArray *secondArr = #[#"Test" , #"Test1", #"Test2"];
NSString * str = #"";
for (int i=0; i< (firstArr<secondArr ? firstArr.count:secondArr.count); i++) {
str = [str stringByAppendingString:[NSString stringWithFormat:#"%# %#,",firstArr[i],secondArr[i]]];
}
str = [str substringToIndex:[str length]-1];
NSLog(#"%#",str);

how to get single characters from string in ios for Gujrati language(other language)

I am trying to get single characters from NSString, like "ઐતિહાસિક","પ્રકાશન","ક્રોધ". I want output like 1)ઐ,તિ,હા,સિ,ક 2) પ્ર,કા,શ,ન 3) ક્રો,ધ, but output is coming like this 1)ઐ , ત , િ , હ , િ , ક 2) પ , ્ , ર , ક , ા , શ , ન 3)ક , ્ , ર , ો , ધ
I have used code like below:
NSMutableArray *array = [[NSMutableArray alloc]init];
for (int i=0; i<strElement.length; i++)
{
NSString *str = [strElement substringWithRange:NSMakeRange(i, 1)];
[array addObject:str];
}
NSLog(#"%#",array);
Let's take strElement as "ક્રોધ" then I got output like this ક , ્ , ર , ો , ધ
But I need output like this ક્રો,ધ
Is there any way that I can get the desired output? Any method available directly in iOS or need to create it by my self then any way or idea how to create it?
Any help is appreciated
Your code is assuming that each character in the string is a single unichar value. But it is not. Some of the Unicode characters are composed of multiple unichar values.
The solution is to use rangeOfComposedCharacterSequenceAtIndex: instead of substringWithRange: with a fixed range length of 1.
NSString *strElement = #"ઐતિહાસિક પ્રકાશન ક્રોધ";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSInteger i = 0;
while (i < strElement.length) {
NSRange range = [strElement rangeOfComposedCharacterSequenceAtIndex:i];
NSString *str = [strElement substringWithRange:range];
[array addObject:str];
i = range.location + range.length;
}
// Log the results. Build the results into a mutable string to avoid
// the ugly Unicode escapes shown by simply logging the array.
NSMutableString *res = [NSMutableString string];
for (NSString *str in array) {
if (res.length) {
[res appendString:#", "];
}
[res appendString:str];
}
NSLog(#"Results: %#", res);
This outputs:
Results: ઐ, તિ, હા, સિ, ક, , પ્ર, કા, શ, ન, , ક્રો, ધ

NSMutableString is null after appending

I am trying to build an array of strings which will be added to an XML soap request.
I have a simple loop to build the array:
MarcTagsList is an NSArray of strings which contains the values ["82a","100a","245b","520a"] and is passed as a parameter.
NSMutableString *xmlTagList;
for(i=0; i<numberOfTags; i++)
{
[xmlTagList appendFormat:#"<string>%#</string>",MarcTagList[i]];
NSLog(#" appending - <string>%#</string>",MarcTagList[i]);
}
The log output shows the loop and MarcTag values are OK, however when the loop is complete
NSLog(#"xmlTagList %#", xmlTagList);
Shows xmlTagList to be null.
Firstly intialize xmlTagList string
//allocated
NSMutableString *xmlTagList=[[NSMutableString alloc] init];
OR
//auto-referenced
NSMutableString *xmlTagList=[NSMutableString string];
OR
//auto-referenced
NSMutableString *xmlTagList=[NSMutableString new];
OR
//auto-referenced
NSMutableString *xmlTagList= #"";
Now use xmlTagList for further requirement ie append operation
for(i=0; i<numberOfTags; i++)
{
[xmlTagList appendFormat:#"<string>%#</string>",MarcTagList[i]];
NSLog(#" appending - <string>%#</string>",MarcTagList[i]);
}
Change:
NSMutableString *xmlTagList;
to:
NSMutableString *xmlTagList = [NSMutableString stringWithString:#""];

How to add values or add object into NSArray in Objective C using for loop?

Well this is my code:
NSDictionary *dicto;
NSString *ndccode;
NSString *string=#" ";
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
string=[string stringByAppendingString:ndccode];
string=[string stringByAppendingString:#"\n"];
NSLog(#"%#",string);
}
NSLog(#"%#",string);
In the above code, i have values in dicto nsdictionary which loops one by one value and assigns to ndccode which is string. Then I am adding to string so that I can append it to the next line.
output :
name1
name2
name3
.....
Instead of assigning to the string. I want to assign it to an array.Could you please tell me how to assign it to an array for this example,since it is in loop. I want to use the array for later purposes.
Thank you in advance.
NSDictionary *dicto;
NSString *ndccode;
NSMutableArray *outputArray=[NSMutableArray new];
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
[outputArray addObject:ndccode];
}
or even more succinctly
NSArray *outputArray = [array valueForKey:#"NDCCode"];

Resources