Strings in Objective C for Iphone - iphone

i am new to iphone application development.
i have a string say abc, i just want to display it as "Hello abc" in the screen
i want to add Hello to abc , before abc.
In objective c, i saw functions appendString , which displays result as "abc Hello"
But i want to display i as "Hello abc"

The easiest way to do it is:
NString *myString = #"abc";
NSString *finalString = [NSString stringWithFormat:#"Hello %#", myString];
NSLog(#"%#", finalString);
this will output "Hello abc".
I say that this is the easiest way, beacause you can reuse this method to add more stuff to the string, like a number:
NSString *playerName = #"Joe";
int num = 5;
[NSString stringWithFormat:#"%# scored %d goals.", playerName, 5];

Try do this:
NSString *string1 = #"abc";
NSString *string2 = [NSString stringWithFormat:#"Hello %#", string1];

You dont need to re-declare the NString variables you can do it via single Mutable variable like.....
NSMutableString *myStr = [NSMutableString string];
[myStr appendString:#"Hello"];
[myStr appendString:#" Cloud"];
int num = 9;
[myStr appendFormat:#" Number %i",num];
NSLog(#"%#",myStr);
and this will print "Hello Cloud Number 9".
Hope this helps.

Related

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

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.

Array in UITextView

I have a text view in which I want to display NSArray *result as text of the text view.
For eg:
result={#"home",#"Office",#"Park",#"Market",nil};
textView text should be:
home
office
park
market
for(int i =0 ; i <[Array Count] ;i++)
{
self.textView.text = [NSString stringWithFormat:#"%# %#",self.textView.text, Array objectAtIndex:i];
}
Correct any spelling mistake.
You can use componentsJoinedByString: method, like this:
NSString *text = [result componentsJoinedByString:#" "];
You can use a different separator instead of #" ".
You can use a for loop as such:
NSString *str =#"";
for (NSString *tmp in result) {
str = [NSString stringWithFormat:#"%# %#",str,tmp];
}
If you want each entry on a new line replace the space with "\n".

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.

extract the first character

i have for exemple "My String" i want to extract the first character .
String *_initialeStr = self.carte.Titre ;
originalCarte.Init = memmove(_initialeStr , _initialeStr+1,length(_initialeStr));
NSString has characterAtIndex: method where you can pass 0 as index...
Use characterAtIndex: from NSString class.
- (unichar)characterAtIndex:(NSUInteger)index
Use as below .
NSString *temp = #"Hello world";
unichar myCharacter = [temp characterAtIndex:0];
That doesn't even look like Objective-C.
Having a property named Init is quite confusing and what is String?
Perhaps this is what you want:
NSString *initialString = self.carte.titre;
originalCarte.initialLetter = [initialString characterAtIndex:0];
This outputs the first character of myString:
NSString *myString = #"The text I want to access.";
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for ( i = 0; i < [myString length]; i++ )
[myArray addObject:[NSNumber numberWithChar:[myString characterAtIndex:i]]];
NSLog( #"First character: %c", [[myArray objectAtIndex:0] charValue] );