How to get desired String from whole - iphone

How to remove string from the original one
i.e. my string is,
series0001.0001
or
series0010.0101
or
series0110.0050
from this string I have to convert it to
expected result
Series 1 1
Series 10 101
Series 110 50
This is the code stuff which I am doing
NSArray * words = [sym.data componentsSeparatedByString:#"\r\n\r\n"];
NSLog(#"words are %#",words);
NSString *strSeriesAndLabelDetail = [words objectAtIndex:0];
NSLog(#"strSeriesAndLabelDetail is %#",strSeriesAndLabelDetail);
NSArray *wordsToSeparateSeriesAndLabel = [strSeriesAndLabelDetail componentsSeparatedByString:#"."];
NSLog(#"wordsSeriesLabel are %#",wordsToSeparateSeriesAndLabel);
strLabelNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:1];
NSLog(#"strLabelNumber are %#",strLabelNumber);
strSeriesNumber = [wordsToSeparateSeriesAndLabel objectAtIndex:0];
NSLog(#"strSeriesNumber is %#",strSeriesNumber);
Current OutPut is:
words are (
"series0001.0003",
"Use the Sort-a-Cord app to read this code or visit www.sortacord.com to get your Sort-a-Cords."
)
strSeriesAndLabelDetail is series0001.0003
wordsSeriesLabel are (
series0001,
0003
)
strLabelNumber are 0003
strSeriesNumber is series0001
Can anybody help me out. Thanks in advance for any suggestion.

A way to do it:
NSString *stringToModify = #"series0110.0050";
stringToModify = [stringToModify stringByReplacingOccurrencesOfString:#"series" withString:#""];
NSArray *array = [stringToModify componentsSeparatedByString:#"."];
NSString *finalString = [NSString stringWithFormat:#"Series %d %d", [[array objectAtIndex:0] integerValue], [[array objectAtIndex:1] integerValue]];
NSLog(#"%#",finalString);
Note that it maybe modified according to what you really want. I assumed that you got always "series" to look for.

Related

Read from txt list file and set many objects in ios

I, I'm writing an application that has to read the content of txt.
this txt is such a property file with a list formatted in this way:
1|Chapter 1|30
2|Chapter AA|7
3|Story of the United States|13
........
keys are separated by "|".
I googled a lot hoping to find any "pragmatically solution" but nothing...
how can I read these informations and set many objects like:
for NSInterger *nChapter = the first element
for NSString *title = the second element
for NSInteger *nOfPages = the last element ?
NSString's - (NSArray *)componentsSeparatedByString:(NSString *)separator could be your best friend.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-componentsSeparatedByString_
Only if you read NSString's class reference:
NSString *str = [NSString stringWithContentsOfFile:#"file.txt"];
NSArray *rows = [str componentsSeparatedByString:#"\n"];
for (NSString *row in rows)
{
NSArray *fields = [row componentsSeparatedByString:#"|"];
NSInteger nChapter = [[fields objectAtIndex:0] intValue];
NSString *title = [fields objectAtIndex:1];
// process them in here
}

How can I get an integer value from NSString in an iPhone application?

NSString * str=[zoneDict objectForKey:#"name"];
NSLog(#"==========string zone::==========%#",str);
// str="(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
How can I get the 3:00 value from the above string?
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSRange endRange = [str rangeOfString:#")"];
NSString *timeString = [str substringWithRange:NSMakeRange(5, endRange.location-5)];
NSRange separatorRange = [timeString rangeOfString:#":"];
NSInteger hourInt = [[timeString substringWithRange:NSMakeRange(0, separatorRange.location)] intValue];
NSLog(#"Hour:%d",hourInt);
Rather than trying to extract the time offset from the string, is there any way you could store actual time zone data in your zoneDict? For example you could store NSTimeZone instances instead.
If all you have is the string, you could use an NSRegularExpression object and extract the relevant information using a regular expression instead.
If you could explain further what you're trying to do then there may be an alternative way to achieve what you want.
I like to use -[NSString componentsSeparatedByString]:
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSArray *myWords = [myString componentsSeparatedByString:#")"];
NSString *temp1 = [myWords objectAtIndex:0];
if ([temp1 rangeOfString:#"-"].location == NSNotFound) {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"+"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
else {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"-"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
Output:
Your String - 3:00
Using regular expressions is the better option in my view (if you are forced to extract the '3' only). The regular expression string would contain something like "\d?" but don't quote me on that, you'll have to look up the exact string. Perhaps someone on here could provide the exact string.

iphone remove next string - leave rest of string after particular occurrence of string

In objective c how to Remove text after a string occurrence.
for example i have to remove a text after occurrence of text 'good'
'iphone is good but..' here i have to remove the but text in the end so the text will be now 'iphone is good'
Try with below code
NSString *str_good = #"iphone is good but...";
NSRange range = [str_good rangeOfString:#"good"];
str_good = [str_good substringToIndex:range.location+range.length];
NSString * a = #"iphone is good but..";
NSRange match = [a rangeOfString:#"good"];
NSString * b = [a substringToIndex:match.location+match.length];
If you want to remove rest of the string after a particular occurrence of "but", you can get the range of "but" and trim the original string down
NSString * test = [NSString stringWithString:#"iphone is good but rest of string"];
NSRange range = [test rangeOfString:#"but"];
if (range.length > 0) {
NSString *adjusted = [test substringToIndex:range.location];
NSLog(#"result %#", adjusted);
}
EDIT
We can assume that the search does not want to cut of "butter is yellow", and can change the range to include " but"
NSRange range = [test rangeOfString:#" but"];
Try this:-
NSArray *array = [string componentsSeperatedBy:#"good"];
NSString *requiredString = [array objectAtIndex:0];
NSArray *array = [string componentsSeparatedByString:stringToSearch];
NSString *requiredString;
if ([array count] > 0) {
requiredString = [[array objectAtIndex:0] stringByAppendingString:stringToSearch];
}

NSRegularExpression to extract text

All,
I have a dictionary with two keys and values. I need to extract bits and pieces from them and place them into seperate strings.
{
IP = "192.168.17.1";
desc = "VUWI-VUWI-ABC_Dry_Cleaning-R12-01";
}
That is what the dictionary looks like when I call description.
I want the new output to be like this:
NSString *IP = #"192.168.17.1";
NSString *desc = #"ABC Dry Cleaning"; //note: I need to get rid of the underscores
NSString *type = #"R";
NSString *num = #"12";
NSString *ident = #"01";
How would I achieve this?
I've read through the Apple developer docs on NSRegularExpression but I find it hard to understand. I'm sure once I get some help once here I can figure it out in the future, I just need to get started.
Thanks in advance.
Okay, so first, you have to get the object associated with each key:
NSString *ip = [dic objectForKey:#"IP"]; //Btw, you shouldn't start a variable's name with a capital letter.
NSString *tempDesc = [dic objectForKey:#"desc"];
Then, what I would do is split the string in tempDesc, based on the character -.
NSArray *tmpArray = [tempDesc componentsSeparatedByString:#"-"];
Then you just have to get the strings or substrings you're interested in, and reformat them as needed:
NSString *desc = [[tmpArray objectAtIndex:2] stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSString *type = [[tmpArray objectAtIndex:3] substringToIndex:1];
NSString *num = [[tmpArray objectAtIndex:3] substringFromIndex:1];
NSString *ident = [tmpArray objectAtIndex:4];
As you can see, this works perfectly without using NSRegularExpression.

NSString Manipulation

I would like to manipulate the NSString like as
(0) Likes (1). (see. (2))
(0) = Raman
(1) = You
(2) = ThisGift
to
Raman Likes You. (see. ThisGift)
I dont know what approch can solve this problem.
Thanks in Advance,
Regards
Venkat.
-[NSString stringByReplacingOccurrencesOfString:withString:].
You use it like this:
NSString * source = #"(0) Likes (1). (see. (2))";
source = [source stringByReplacingOccurrencesOfString:#"(0)" withString:#"Raman"];
NSLog(#"%#", source); //logs "Raman Likes (1). (see. (2))"
If you're allowed to change the template format, you can use format strings.
NSString *template1 = #"%1$# Likes %2$#. (see. %3$#)",
*template2 = #"%2$# got a %3$# from %1$#.";
NSString *msg1 = [NSString stringWithFormat:template1,#"Raman",#"You",#"ThisGift"],
*msg2 = [NSString stringWithFormat:template2,#"Raman",#"You",#"ThisGift"];
or (if the format string can always depend on the arguments being in replacement order):
NSString *template = #"%# Likes %#. (see. %#)";
NSString *msg = [NSString stringWithFormat:template,#"Raman",#"You",#"ThisGift"];
See -[NSString stringByReplacingOccurrencesOfString:withString:]
NSString *foo = #"(0) likes (1). (see (2))";
NSString *bar;
bar = [foo stringByReplacingOccurrencesOfString:#"(0)"
withString:#"Raman"];
bar = [bar stringByReplacingOccurrencesOfString:#"(1)"
withString:#"You"];
bar = [bar stringByReplacingOccurrencesOfString:#"(2)"
withString:#"ThisGift"];