iOS - parse a string using whitespace - ios

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

Related

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

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.

objective-c regex to remove NSString characters if they start with spaces and commas

My app uses addresses. I am not sure how to create a regex that strips away an address that reads like these.
" , Paxton, TX"
" , Dallas TX"
Need the above to be stripped so they read like this.
"Paxton, TX"
"Dallas TX"
Use the NSString API stringByTrimmingCharactersInSet:
NSString *test = #" , Paxton, TX";
NSMutableCharacterSet *charset = [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
[charset addCharactersInString:#","];
NSString *final = [test stringByTrimmingCharactersInSet:charset];
NSLog(#"final = [%#]",final);
You can also do this without using regular expressions
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#" ,"];
NSString *trimmed = [#" , Payton, TX" stringByTrimmingCharactersInSet:charSet];
NSLog(#"%#", trimmed); //prints "Payton, TX"

IOS NSString get characters before '#"

for example i have string like this:
NSString *one = B3#This is the first string
NSString *two = 1#This is the second string
How can i get the "B3" and "1" Character only (using objective C)
Thanks..
Turns out this is one way to do it:
NSRange range = [one rangeOfString:#"#" options:NSBackwardsSearch];
NSString *newString = [one substringToIndex:range.location];
Thanks for all the answers.
NSString* one = #"B3#";
NSString* two = #"1#";
NSString* result = [one stringByReplacingOccurrencesOfString:#"#" withString:#""];
NSString* result_2 = [two stringByReplacingOccurrencesOfString:#"#" withString:#""];
//if you need to marge
NSString* tot = [NSString stringWithFormat:#"%#%#",result,result_2];

How to concatenate NSArray values by excluding brackets?

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.

Resources