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

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

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 Ten Spaces After Specific Character

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

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

Replacing characters in an NSString after converting an NSDictionary to an NSString

I've searched around and couldn't find an answer, so forgive me if I've created a duplicate question.
I've converted an NSDictionary into an NSString and would like to take out all the occurrences of "{", "}", and ";". When I use stringByReplacingOccurrencesOfString nothing appears different in the NSLog.
The code I've written:
NSString *myString = [myDictionary description];
[myString stringByReplacingOccurrencesOfString: #"{" withString:#""];
[myString stringByReplacingOccurrencesOfString: #"}" withString:#""];
[myString stringByReplacingOccurrencesOfString: #";" withString:#""];
NSLog(#"%#", myString);
The reason that nothing appears different is that the stringByReplacingOccurencesOfString returns a new string that you need to assign to myString, it doesn't mutate the string in place, so you could use
NSString *myString = [myDictionary description];
myString=[myString stringByReplacingOccurrencesOfString: #"{" withString:#""];
myString=[myString stringByReplacingOccurrencesOfString: #"}" withString:#""];
myString=[myString stringByReplacingOccurrencesOfString: #";" withString:#""];
NSLog(#"%#", myString);
But, generally the use of description for anything other that debugging/logging should be avoided. You should iterate the dictionary directly - For example.
NSMutableString *myString=[NSMutableString new];
for (NSString *key in myDictionary.allKeys) {
[myString appendString:#"%# = %#\r\n",key,myDictionary[key]];
}
NSLog(#"%#",myString");
You need to use stringByTrimmingCharactersInSet and stringByReplacingOccurrencesOfString api both. Refer the below sample code:-
NSString *myString = [myDictionary description];
NSString *str=[myString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"{}"]];
NSLog(#"%#", [str stringByReplacingOccurrencesOfString:#";" withString:#""]);

How to replace special characters in NSString with unknown index

I am trying to replace some characters with unknown index like this(i need to replace this Ă,Ŏ,Ĭ,ă,ŏ,ĭ and my input nsstring can be anything):
(void)repairText:(NSString *)textToRepair{`
NSString *pom = textToRepair;`
int pomNum = [pom length];
NSLog(#"Input nsstring: %#",pom);
for (int a = 0; a<pomNum; a++) {
NSString *pomChar, *pomChar2;
pomChar = [pom substringFromIndex:a];
pomChar2 = [pomChar substringToIndex:(1)];
NSLog(#"Char to repair: %#",pomChar2);
if ([pomChar2 isEqual: #"Ă"] || [pomChar2 isEqual:#"Ŏ"] || [pomChar2 isEqual:#"Ĭ"] || [pomChar2 isEqual:#"ă"] || [pomChar2 isEqual:#"ŏ"] || [pomChar2 isEqual:#"ĭ"]) {
if ([pomChar2 isEqual:#"Ă"]) {
NSLog(#"Wrong big a");
}
if ([pomChar2 isEqual:#"Ŏ"]) {
NSLog(#"Wrong big o");
}
if ([pomChar2 isEqual:#"Ĭ"]) {
NSLog(#"Wrong big i");
}
if ([pomChar2 isEqual:#"ă"]) {
NSLog(#"Wrong small a");
}
if ([pomChar2 isEqual:#"ŏ"]) {
NSLog(#"Wrong small o");
}
if ([pomChar2 isEqual:#"ĭ"]) {
NSLog(#"Wrong small i");
}
} else {
NSLog(#"Good");
}
}
pom = [textToRepair stringByReplacingOccurrencesOfString:#"•" withString:#" kulka "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"¥" withString:#" jen "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"£" withString:#" libra "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"€" withString:#" euro "];
[self synthesize:pom];
}
But I am having trouble with 'if'. If anyone know about this, please help in this regard.
NSString *str=#"ĂdsdaĬsd";
str=[str stringByReplacingOccurrencesOfString:#"Ă" withString:#""];
str=[str stringByReplacingOccurrencesOfString:#"Ĭ" withString:#""];
NSLog(#"%#",str);
O/p :dsdasd
NSString has functions to do that for you. dataUsingEncoding:allowLossyConversion: is the method you need.
From the documentation:
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)flag
If flag is YES and the receiver can’t be converted without losing some information, some characters may be removed or altered in conversion. For example, in converting a character from NSUnicodeStringEncoding to NSASCIIStringEncoding, the character ‘Á’ becomes ‘A’, losing the accent.
Sample code:
NSString *str = #"á, é, í, ó, ú, ü, ñ";
NSData *asciiStringData = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *finalString = [[NSString alloc] initWithData:asciiStringData
encoding:NSASCIIStringEncoding];
The final string will be : a, e, i, o, u, u, n
NSString * textToRepair = #"Your String";
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"•" withString:#"kulka"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"¥" withString:#"jen"]
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"£" withString:#"libra"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"€" withString:#"euro"];;
and now textToRepair is your output string with changes.
You can used this:
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
I hope this is used full to you.

Resources