how to give space between two string while concatnating - iphone

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

Related

how to use stringByTrimmingCharactersInSet in NSString

I have a string which gives the Date (below)
NSString*str1=[objDict objectForKey:#"date"];
NSLog(#" str values2%#",str1); --> 04-Jan-13
Now Problem is I need to Trim the"-13" from here .I know about NSDateFormatter to format date.but I can't do that here.I need to trim that
For that I am using:-
NSCharacterSet *charc=[NSCharacterSet characterSetWithCharactersInString:#"-13"];
[str1 stringByTrimmingCharactersInSet:charc];
But this does not work.this does not trim...how to do that..help
Not sure why not use an NSDateFormatter but here's a very specific way to approach this (very bad coding practice in my opinion):
NSString *theDate = str1;
NSArray *components = [theDate componentsSeparatedByString:#"-"];
NSString *trimmedDate = [NSString stringWithFormat:#"%#-%#",[components objectAtIndex:0],[components objectAtIndex:1]];
But this does not work.this does not trim...
It does trim, but since NSString is immutable, the trimmed string is thrown away, because you do not assign it to anything.
This would work (but do not do it like that!)
str1 = [str1 stringByTrimmingCharactersInSet:charc];
What you do is not trimming, it's taking a substring. NSString provides a much better method for that:
str1 = [str1 substringToIndex:6]; // Take the initial 6 characters
Something like this:
NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
or this:
NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"-13"];
what you have done is correct. Only thing is stringByTrimmingCharactersInSet returns NSString. So you need to assign this value to NSString, like
str1 = [str1 stringByTrimmingCharactersInSet:charc];
if you're sure you have your string always formatted like "NN-CCC-NN" you can just trim the first 6 chars:
NSString* stringToTrim = #"04-Jan-13";
NSString* trimmedString = [stringToTrim substringToIndex:6];
NSLog(#"trimmedString: %#", trimmedString); // -> trimmedString: 04-Jan

NSString select part go a string objective-c

I have problem with string. The string shows: ~00000000:termometr2: +26.9 st.C and I want to use only this part: +26.9 st.C in my textfield.text.
Thanks
NSString *fullStr = #"00000000:termometr2: +26.9 st.C";
NSArray *parts = [fullStr componentsSeparatedByString:#" "];
textField.text =[NSString stringWithFormat:#"%#",[parts objectAtIndex:1]];
it might help you:
NSArray *_array = [yourString componentsSeparatedByString:#":"];
[myTextField setText:[_array lastObject]]; // or any other component you want
Just do it this way, using the method stringByReplacingOccurrencesOfString: withString::
NSString *originalString = #"~00000000:termometr2: +26.9 st.C";
NSString *filteredString = [originalString stringByReplacingOccurrencesOfString:#"~00000000:termometr2: " withString:#""];

Adding Onto NSString

I have an NSString that I would like to add extra characters to. In my mind I thought it would be something simple like this:
NSString *answerString = [NSString stringWithFormat:#"%f", finalVolume] + #" Cubic Feet";
But that did not work. Does anyone know what I might be missing here? Thanks!
NSString is immutable, so you cannot just add to it. Instead, you either compose your string like this:
NSString *answerString = [NSString stringWithFormat:#"%f Cubic Feet", finalVolume];
or
NSString *unit = #"Cubic Feet";
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume, unit];
or create a mutable one:
NSMutableString *answerString = [NSMutableString stringWithFormat:#"%f ", finalVolume];
[answerString appendString:#"Cubic Feet"];
Use NSMutableString
You can append anything you like...
I am pretty sure you can do the following:
NSString *answerString = [NSString stringWithFormat:#"%f Cubic Feet", finalVolume];
or if the part being appended needs to be variable you can do the following:
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume, myVariable];
[[NSString stringWithFormat:#"%f", finalVolume] stringByAppendingString:#" Cubic Feet"];
Simply use
NSString *answerString = [NSString stringWithFormat:#"%f %#", finalVolume,#"Cubic Feet"];
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString:#"s1"];
[str appendString:#"s2"];
[NSString stringwithformat:#"%f %#", final value, #"cubic feet"];

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