Remove substring before a specific char (NSString) - objective-c

Basically I am getting a users city throughout Facebook Connect. The result is always in this format:
Sydney, Australia
But since there will be many users, the city and country can vary. Here is the format:
First the city with a comma, then a space and the country.
I only want to however retrieve the country name. Is it possible for me to locate the comma in string, then remove everything behind it. Then I can replaceOccurancesOfString:" " to remove the space.
I have seen other questions on this but they are in different languages. I am asking for Objective-C
Thanks!

one easy way to do it is to use - (NSArray *)componentsSeparatedByString:(NSString *)separator to make an array out of it, and then pick the second object of the array.

Related

Kotlin functional programming keep reference of previous object in List

I have a list of Person objects which are related to each other with spouse relation in the order of which they appear in the list.
enum class Gender {
MAN, WOMAN
}
data class Person(val name: String, val age: Int, val gender: Gender)
In the list, each Person with Gender MAN is the spouse of the following Person with WOMAN Gender (and vice versa) and each entry in the list is followed by alternating genders with MAN gender be the first.
The list should ideally be like [MAN, WOMAN, MAN, WOMAN, MAN, WOMAN] (obviously it will be a list of Person objects for simplicity I am putting a list of Gender here) but it could also be like [WOMAN, MAN, WOMAN, MAN, WOMAN, MAN]. In the latter case, the first appearing WOMAN is the spouse of the last appearing MAN.
How this second case could be handled in kotlin by using functional programming.
My current approach involves checking if the first Person has a gender women then i remove the first and last objects in the list and then add them in the end but this is not fully a functional programming solution.
Anyone can guide me about that?
Thanks
What do you mean by fully functional approach?
Similar to what you mentioned, you can fix the order by a simple statement like this:
val correctList = if(list.first().gender == MAN) list else list.drop(1) + list.first()
If you want a more general approach, you can do something like this:
// separate the people into a list of gender=MAN and a list of everyone else
// the result is a Pair, so I'm destructuring that into two variables
val (men, women) = people.partition { it.gender == MAN }
// use zip to take a person from each list and combine them into a list of pairs
men.zip(women)
// use flatMap to turn each pair into a list of two people
// just using map would create a list of lists, flatMap flattens that into
// a single list of people, alternating gender=MAN, gender=WOMAN
.flatMap { it.toList() }
This way it doesn't matter how your original list is ordered, it can start with any element and you can have the different types completely mixed up - BABBAABA will still come out as ABABABAB. So it's a general way to combine mixed data streams - partition separates them into groups, and zip lets you take an element from each group and do something with them.
Here I'm just letting zip create Pairs, and then flatMap turns those back into an ordered list (if that's what you want). You could also do a forEach on each pair instead (say if you wanted to set a value on each Person to link them to each other), or zip can take a transform function too.
Also zip terminates when one of the lists runs out (e.g for AAA and BB you'll get two pairs) so this works for generating complete pairs of elements - if you also needed to handle elements without a "partner" you'd need to do a bit more work

Method converts strings in Objective-C

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.

Strip first 2 characters and replace with another

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"

Localized phone number formatting

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.

substring and the indexOf method

My assignment in Visual Basic 2010 is to build a order form that has two text boxes, one for the name and the other for the address.
And we're suppose to use the IndexOf method on the address.
I understand that IndexOf returns the position of a character and the number of characters.
What would be the purpose of using the IndexOf method on the address in this instance?
I don't understand what I would be searching for when the user types in it's address that's going to be numbers and string characters.
I think I understand what the IndexOf method does, somewhat, but why and what would I use it to achieve?
You'd typically use IndexOf to -
See if a string contained something
If someString.IndexOf("Avenue") > - 1 Then
'do something
End If
Get the start position of a value in a string , this could then be used to extract part of the string. e.g. someString.Substring(someString.IndexOf("#"),10) would get then next ten characters starting from where the "#" character was found in your string.
Bear in mind you'll always need to handle scenarios where IndexOf will return -1 if it does not find the string your searching for, so your code will have to handle that eventuality.
Since this is a homework question, I will answer with a question or several:
How would you normally enter a full address into a text box?
How would each part of the address be distinguishable from another?
Once you figure out how this can be done, think about IndexOf again.