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]
}
Related
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:#"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:#"-" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:#"(" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#")" withString:#""];
txtRecipient.text = contactString;
}
This is my code. I am using contactPicker to pick a contact from phonebook. Then I am storing it in to a string variable. After that I am removing dashes, brackets and spaces from string value by using stringByReplacingOccurrencesOfString. Every thing is working fine except for this line:
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""];
After this line of code contactString remains the same i.e, spaces didn't get removed from the string. I also tried componentsSeparatedByString function but its returning only 1 character. i.e,
NSArray *components = [dateString componentsSeparatedByString:#" "];
components.length is returning 1.
Hope you understand my question. Is there any other way of removing spaces from a string? Any kind of help would be appreciable. Thanks.
Look like this is not a standard space.
Try this:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:#"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:#"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}
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 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 got iOS deviceToken like this
<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>
now , I want delete the blank-space and "<" ">" , to get to
72c7f0e943d336713b827e234337e391a96873210d2eecc4
what can I do for this ?
The other question is
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
NSLog(#"%#",deviceString);
}
but the output is "null" , why ??
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
token = [token stringByReplacingOccurrencesOfString:#" " withString:#""];
You ask why this didn't work:
NSString *deviceString = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
That's because the initWithData assumes that the NSData contains a UTF8 string. But in your case, the deviceToken is not a UTF8 string; it is binary data.
The rest of your question presumes that you will create the <72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4> string (presumably that you created with stringWithFormat or description methods), and you're asking how to trim the <, >, and remove the spaces. (I think others have answered that question.)
I might suggest a different approach, though. You could simply have a routine to create the hexadecimal string directly, like so:
NSString *deviceString = [self hexadecimalStringForData:deviceToken];
You could then have a hexadecimalStringForData method like so:
- (NSString *)hexadecimalStringForData:(NSData *)data
{
NSMutableString *hexadecimalString = [[NSMutableString alloc] initWithCapacity:[data length] * 2];
uint8_t byte;
for (NSInteger i = 0; i < [data length]; i++)
{
[data getBytes:&byte range:NSMakeRange(i, 1)];
[hexadecimalString appendFormat:#"%02x", byte];
}
return hexadecimalString;
}
You certainly can use the description/stringWithFormat approach and then clean up that string, as others have suggested, but the above strikes me as a more direct solution.
You can use below code for this:
NSString *newString1 = [deviceString stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *newString2 = [newString1 stringByReplacingOccurrencesOfString:#"<" withString:#""];
NSString *finalString = [newString2 stringByReplacingOccurrencesOfString:#">" withString:#""];
NSlog("%#",finalString);
Simply use this code
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString *devToken;
devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString:#" "withString:#""];
NSLog(#"%#",devToken);
}
To replace any character/string in any string, you can use :
[yourString stringByReplacingOccurrencesOfString:strToBeReplaced withString:strToBeReplacedWith];
In your case, you can use this:
NSString * str= #"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";
str = [str stringByReplacingOccurrencesOfString:#"<" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#">" withString:#""];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
You can use this also (Taken from this question):
NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
Use this code.
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
self.deviceToken = [newToken stringByReplacingOccurrencesOfString:#" " withString:#""];
}
Write below method in your view controller .m file
-(NSString*)contentsInParenthesis:(NSString *)str
{
NSString *subString = nil;
NSRange range1 = [str rangeOfString:#"<"];
NSRange range2 = [str rangeOfString:#">"];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
{
NSRange range3;
range3.location = range1.location+1;
range3.length = (range2.location - range1.location)-1;
subString = [str substringWithRange:range3];
}
return subString;
}
And use this method like:
NSString *string = #"<72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4>";
NSString *str=[self contentsInParenthesis:string];
Now, Printing description of str:
72c7f0 e943d3 36713b 827e23 4337e3 91a968 73210d 2eecc4
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.