I'm new to objective-c and I need help with this.
A phone number in this country has 10 digits like 6195946191 or 619JYDN191. It is hard to read a phone number formatted like that. One common format is 619-594-6191. Add the method phoneFormat to the NSString class. The method phoneFormat converts strings like
#"6195946191", #"619 594 6191", #"619 5946191" and #"619-594-6191" to #"619-594-6191".
That is all the methods below will return #"619-594-6191"
[#"6195946191" phoneFormat]
[#"619 594 6191" phoneFormat]
[#"619 5946191" phoneFormat]
[#"619-594-6191" phoneFormat]
Can anyone show me how to do this?
Do it in two stages. First remove all undesirable characters (everything that is not a digit? depends what input you are expecting). Personally, I would probably use NSScanner for this, though I can think of other ways.
Now you are guaranteed you have 10 digits in succession, so insert a hyphen after the 3rd and 6th characters and you're done.
Related
I'm having difficulties as to how I should parse this kind of csv file.
For example:
06:16 PM,7,299,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0026_heavy_rain_showers_night.png,Moderate rain at times,14,22,180,S,3.1,81,10,993,75
2014-01-31,9,48,3,38,22,35,176,S,119,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png,Cloudy,6.0
2014-02-01,7,45,3,37,19,30,220,SW,113,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png,Sunny,2.2
2014-02-02,9,47,3,37,17,27,236,SW,113,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png,Sunny,0.0
2014-02-03,8,46,3,37,21,34,152,SSE,116,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png,Partly Cloudy,1.8
2014-02-04,9,48,3,38,20,32,191,SSW,263,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png,Patchy light drizzle,1.4
"London","United Kingdom","City Of London, Greater London",51.517,-0.106,7421228,http://www.worldweatheronline.com/London-weather/City-of-London-Greater-London/GB.aspx
For example, I need to get the first two values on the first line, all the values on the 2nd to 6th line, and the first value of the 7th line.
I currently have a model class with properties for all the values I need to get.
I'm not sure how to do it in this situation. So far, I know how to parse that csv (if I didn't need to get the first two values on the 1st line, and the first value of the 7th line)
What would be the logic to parse in this situation? Hope you guys can give me some idea how to do it.
Thanks
Split the string into lines (busing a scanner or array method depending on how big the string is). Once you have your lines, takes the special ones and pass to appropriate methods to extract the required values. Likewise for the main lines.
You can use your parser on each line individually or use a scanner or array method if that's easier depending on what content you need to extract and where it is.
Anyone knows how to do it?
So we split that based on space, and then capitalize each, and then move them back right?
You can use either of these methods to accomplish that:
-[NSString capitalizedString]
Or:
-[NSString capitalizedStringWithLocale:]
Just use the capitalizedString method.
Return Value
A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.
To capitalize use :
string=[string capitalizedString];
Gives each word of the sentence in capital.
So we split that based on space, and then capitalize each, and then
move them back right?
This is not required. No need to break in words and individually capitalize each one of them.
This seems like quite a silly basic question but it seems something I have completely passed over in my knowledge.
Basically I have a string representing a phone number (0 being the area code): 0828889988
And would like to replace the first zero with a 27 (South African dialing code) as I am pretty sure it will always be so, my SMS api requires it in international format but I want the user to enter it in local format, so should be: 27828889988
Is there a line or two of code I can call to replace that first character with the two others?
As is - I can think around a workaround solution but as I am not sure of the direct syntax will be quite a few lines long.
Dim number as String = "0828889988"
number = "27" + number.SubString(1)
return number //returns "27828889988"
I've looked into NSFormatter, NSNumberFormatter, and the other formatting classes, but can't find a build into solution. I need to format phone numbers depending on the country code.
For instance, for US, I get a string such as +16313938888 which I need to format to look like +1(631)393-8888. The problem is I need to do this for all formats. Netherlands, I receive a string +31641234567 which will be +31(6)41 23 45 67 (something like that).
Hardcoding for 200+ countries is too tedious and I really don't know all the format rules. Is there something in the docs I'm overlooking or does anyone know of an open source class that manages this?
See https://github.com/rmaddy/RMPhoneFormat for an iOS specific solution.
Try this Google solution - https://github.com/me2day/libPhoneNumber-iOS
They have ports for C++, Java, Objective-C and others.
Unfortunately iOS does not have any public APIs for this. You can try to integrate libphonenumber that is a complete implementation for parsing and formatting international phone numbers. It has a C++ version so theoretically you can cross-link with it.
You definitely don't want to hard-code all of the various country formats. There are typically 3-5 formats per country. Instead, use a format database (such as a plist) and write code to format the number based on the given country code.
A good international format property list 'UIPhoneFormats.plist' can be found here: https://code.google.com/p/iphone-patch/source/browse/trunk/bgfix/UIKit.framework/PhoneFormats/UIPhoneFormats.plist?r=7
In that list, '$' allows any character, '#' must be a number, and the '(space) ', '(', ')' and '-' are inserted between numbers. Non-numeric characters typed by the user hint to the desired format.
I've shared my phone number formatter class, inspired by Ahmed Abdelkader's work, at https://github.com/lathamglobal/iOS-Phone-Number-Formatter . It is a very small, single-class international phone number formatter that uses the plist just mentioned.
You can try this:
let phoneNumber : CNPhoneNumber
let digits = phoneNumber.performSelector("digits").takeRetainedValue() as! String
It gives you directly the string, without formatting, with the phone number. However if the number is saved with international prefix, you will have it also in the resulted string.
I have several directories of 12 .caf files and am loading them programmatically:
NSString *soundToPlay = [NSString stringWithFormat:#"sounds/%d/%d_%d.caf", type, note, timbre];
If I want to, say, increment from 9 to 10 in one of those values, suddenly my string is an extra character long, which makes it harder to manipulate later with something like NSMakeRange. I'd like to keep all these %ds to a single character.
What I'd like to do is name my files using the digits 0-9 but then continue with A, B, C instead of 10, 11, 12. This would keep everything single-character. I'm hoping there's an easy way to do this kind of thing, still allowing stuff like increment, +/-, and modulo. If so, what is it?
X is the hexadecimal format specifier:
[NSString stringWithFormat:#"sounds/%X/%X_%X.caf", type, note, timbre]
Alternatively you could always use two digit numbers. That would allow to select from more than 16 cases:
[NSString stringWithFormat:#"sounds/%02d/%02d_%02d.caf", type, note, timbre]