Remove Ten Spaces After Specific Character - ios

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 ...

Related

stringByReplacingOccurrencesOfString is not working for space i.e, " " in objective c

- (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];
}

Remove particular String from NSArray not Object

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]
}

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.

How to delete the blank-space and “< >”

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

iOS - parse a string using whitespace

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"}

Resources