My array is like this
(1,2,3,)
(4,5,6,)
(7,8,9,)
I want to concatenate all these values i.e;123456789
I tried like this NSString *str5=[array componentsJoinedByString:#" "];
but I'm not getting the output.
can anyone provide me some information.
Looks like you have an NSArray of NSArray. First loop through the original NSArray and flatten it then apply componentsJoinedByString.
Create an NSMutableArray, loop through your original array and call addObjectsFromArray: with each subarray.
NSMutableArray *flattenArray = [[NSMutableArray alloc] init];
for(NSArray *array in originalArray)
{
[flattenArray addObjectsFromArray: array];
}
NSString *str5 = [flattenArray componentsJoinedByString:#" "];
This should work.
try like this it'l give expected output but i don't know is it efficient method or not ,
NSArray *arr=[[NSArray alloc]initWithObjects:#"(1,2,3)",#"(4,5,6)", nil];
NSString *str=[arr componentsJoinedByString:#""];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#",()"];
str = [[str componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"%#",str);
Try this for Character replacing brackets with null values.
Code ::
NSArray *arr = [[NSArray alloc] initWithObjects:#"(1, 2, 3, )", #"(4, 5, 6, )", #"(7, 8, 9, )", nil];
for (int i = 0; i < [arr count]; i++) {
NSLog (#"==> %#", [or objectAtIndex:i]);
}
NSString *str5 = [arr componentsJoinedByString:#" "];
NSLog(#" ==> %#", str5);
str5 = [str5 stringByReplacingOccurrencesOfString:#"(" withString:#""];
str5 = [str5 stringByReplacingOccurrencesOfString:#" " withString:#""];
str5 = [str5 stringByReplacingOccurrencesOfString:#")" withString:#""];
str5 = [str5 stringByReplacingOccurrencesOfString:#"," withString:#""];
//Or Try this
NSCharacterSet *doNotWantChars = [NSCharacterSet characterSetWithCharactersInString:#",() "];
str5 = [[str5 componentsSeparatedByCharactersInSet: doNotWantChars] componentsJoinedByString: #""];
NSLog(#" ==> %#", str5);
NSLog(#"Output :: %#", str5);
I've replaced "(", ")", " " & "," with ""(null) values
Hopefully, it'll help you.Thanks.
Related
I have an string which is coming from a server :
<p>(555) 555-5555 </p>
I want to remove any space after "tel:" up to 10 characters.
I tried this below code but didn't achieve my goal.
NSString *serviceMessage = dict[#"message"];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"<br />" withString:#"\n"];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"<div>" withString:#"\n"];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"<p>" withString:#""];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"</div>" withString:#""];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"</p>" withString:#""];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#" " withString:#""];
serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:#"\n" withString:#" "];
NSString *mainString = serviceMessage;
if ([mainString containsString:#"tel:"]) {
NSUInteger location = [mainString rangeOfString:#"tel:"].location + 4;
NSString *newString = [mainString substringFromIndex:location];
[newString substringWithRange:NSMakeRange(10,0)];
NSString *newStr =[newString substringToIndex:12];
NSLog(#"New Trimed string:%#",newStr);
newStr = [newStr stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"Final Trimed string:%#",newStr);
}
serviceMessage = [serviceMessage stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *from = #"From : ";
NSString *dealerName = dict[#"messageFrom"];
NSString * append = [NSString stringWithFormat:#"%#%#%#%#",from, dealerName,#"\n",serviceMessage];
Try this
NSString *str1=#"Your Example No = 1";
NSArray *tempArray = [str1 componentsSeparatedByString:#"="];
str1 = [tempArray objectAtIndex:0];
NSLog(#"%#", str1);
Output is Your Example No
Hope this helps
If you want the telephone number after the "tel:", you can do it in following way
My code Snippet :
NSString *str = #"<p>(555) 555-5555 </p> ";
NSLog(#"str = %#", str);
//It removes the unwanted extra character like (, ), -
NSCharacterSet *dontInclude = [NSCharacterSet characterSetWithCharactersInString:#"()-"];
NSString *strRemoveSpecial = [[str componentsSeparatedByCharactersInSet: dontInclude] componentsJoinedByString: #""];
//Now strRemoveSpecial is without those special character and we remove the space from the resulted string here
NSString *str1 = [strRemoveSpecial stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"str1 = %#", str1);
//Here we separate string by "tel:" and takes the last part, which contains the telephone number
NSString *strAfterTel = [[strRemoveSpecial componentsSeparatedByString:#"tel:"] lastObject];
//Now substring to 10, which gives us the required telephone number.
NSString *firstTemAfterTel = [strAfterTel substringToIndex:10];
NSLog(#"firstTemAfterTel : %#", firstTemAfterTel);
Hope it helps.
Happy coding ...
I am new in IOS development.I am getting phone numbers in NSArray and this is something like this.
9429564999,
9428889239,
7878817072,
"+919408524477",
9909951666,
9879824567,
"+91 8469-727523",
"94-28-037780",
"+918460814614",
55246,
"8866-030880",
"95-37-223347",
"+919574777070",
"+917405750526",
Now i want to remove - and +91 from NSArray List not whole object.How to do this Please someone help
for (NSString *number in arrayName) {
number = [number stringByReplacingOccurrencesOfString:#"+91"
withString:#""] mutableCopy];
number = [number stringByReplacingOccurrencesOfString:#"-"
withString:#""] mutableCopy];
[arrayNameNew addObject:number];
}
Try like this :)
Happy Coding.
for (NSString *phoneNum in phoneNumArray) {
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#"+91" withString:#""];
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#"-" withString:#""];
}
NSArray *phoneNoList = [[NSArray alloc]initWithObjects:#"9429564999",#"9428889239",#"7878817072",#"+919408524477",#"9909951666",#"9879824567", #"+91 8469-727523",#"94-28-037780",#"+918460814614",#"55246",#"8866-030880",#"95-37-223347",#"+919574777070", #"+917405750526", nil];
NSMutableArray *noArr = [[NSMutableArray alloc]init];
for (NSString *no in phoneNoList) {
NSString *temp1 = [[no componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
if (temp1.length > 10) {
NSString *temp=[temp1 substringFromIndex:2];
[noArr addObject:temp];
}
else
[noArr addObject:temp1];
}
NSLog(#"Arr Data : %#",noArr);
Try this, definitely helps to you.
Try this
for (NSString *str in arrayNumbers) {
NSString *stringWithoutNineOne = [str stringByReplacingOccurrencesOfString:#"+91" withString:#""];
NSString *stringWithoutSpace = [stringWithoutNineOne stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *stringWithoutdash = [stringWithoutSpace stringByReplacingOccurrencesOfString:#"-" withString:#""];
[refinedArr addObject:stringWithoutdash];
}
Remove special characters from array.
for (NSString *strPhoneNo in arrayPhoneNo) {
[self removeSpecialCharacter:strPhoneNo];
}
and
-(NSString*)removeSpecialCharacter:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+91" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
// remove extra special charecter if needed.
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
return mobileNumber;
}
if NSArray not update then use NSMutableArray insted of NSArray.
You can also try this....
Getting the last 10 digits of your given phone numbers.....if your requirement is fixed then try this single line code
for (NSString *string in arrayNumbers) {
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-10, 0)];
[newArray addobject:trimmedString]
}
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.
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.
I am an iOS newbie, so please pardon me if this is a beginner level question.
I have a string "hu_HU Hungary:Hungarian" and I want to put it into an array like {"hu", "HU", "Hungary:Hungarian"}. How would I parse it to remove the whitespace and then the underscore?
Use componentsSeparatedByCharactersInSet: method of NSString
To get the desired effect you must change the '_' to a ' ' or vice versa. Then you can use componentsSeparatedByString, like so:
NSString *source = #"hu_HU Hungary:Hungarian";
source = [source stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSArray *components = [source componentsSeparatedByString:#" "];
NSLog(#"%#",components);
NSString *theStr = #"hu_HU Hungary:Hungarian";
NSArray *pieces = [theStr componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: #"_ "]];
//pieces = {"hu", "HU", "Hungary:Hungarian"}
pieces = [theStr componentsSeparatedByCharactersInSet: [[NSCharacterSet alphanumericCharacterSet] invertedSet]];
//pieces = {"hu", "HU", "Hungary", "Hungarian"}
//the motivation for this is a better defined set of non-alphanumeric characters
Here you are:
NSString *myString = #"hu_HU Hungary:Hungarian";
myString = [myString stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSArray *myArray = [myString componentsSeparatedByString:#" "];
All of these answers share an annoying problem. They assume that the items are separated by one and only one space.
NSString *theStr = #"hu_HU Hungary:Hungarian";
NSArray *pieces = [theStr componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: #"_ "]];
//pieces = {"hu", "HU", "Hungary:Hungarian"}
NSString *theStr = #"hu_HU Hungary:Hungarian"; // note extra space
NSArray *pieces = [theStr componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: #"_ "]];
//pieces = {"hu", "HU", " ", "Hungary:Hungarian"}