NSString Manipulation - iphone

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"];

Related

How to get desired String from whole

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.

how to give space between two string while concatnating

I am concatenating two string using following way I want space between Added Content To My Learning And the second String any idea how to give space thanks
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [firstString stringByAppendingString:secondString];
You can do it like this.
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [NSString stringWithFormat:#"%# %#", firstString, secondString];
Did you try?
NSString *firstString =#"Added Contnet To My Learning ";
// a space after the first string
Here is all way you can do with string
//1st Way
NSString *finalString = [NSString stringWithFormat:#"%# %#",firstString,secondString];
//2nd Way
NSString *finalString = [firstString stringByAppendingFormat:#" %#",secondString];
//3rd way
NSArray *ary= [NSArray arrayWithObjects:firstString,secondString, nil];
NSString *finalString= [ary componentsJoinedByString:#" "];
Follow this how to concatenate two strings in iphone

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
}

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.

Get last path part from NSString

Hi all i want extract the last part from string which is a four digit number '03276' i:e http://www.abc.com/news/read/welcome-new-gig/03276
how can i do that.
You can also use
NSString *sub = [#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent];
If you know how many characters you need, you can do something like this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [string substringFromIndex:[string length] - 5];
If you just know that it's the part after the last slash, you can do this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [[string componentsSeparatedByString:#"/"] lastObject];
Since *nix uses the same path separators as URL's this will be valid as well.
[#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]
If you know the length of the number, and it's not gonna change, it can be as easy as:
NSString *result = [string substringFromIndex:[string length] - 4];
If the last part of the string is always the same length (5 characters) you could use this method to extract the last part:
- (NSString *)substringFromIndex:(NSUInteger)anIndex
Use the length of the string to determine the start index.
Something like this:
NSString *inputStr = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *newStr = [inputStr substringFromIndex:[inputStr length]-5];
NSLog(#"These are the last five characters of the string: %#", newStr);
(Code not tested)
NSString *str = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSArray *arr = [str componentSeparatedBy:#"gig/"];
NSString *strSubStringDigNum = [arr objectAtIndex:1];
strSubStringDigNum will have the value 03276
Try this:
NSString *myUrl = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *number = [[myUrl componentsSeparatedByString:#"/"] objectAtIndex: 5];