append in middle of NSString in iPhone programming - iphone

how to append the value of string in between NSString?
for example:
NSString* str1 = #"Hello";
NSString* str2 = #"Hi.."/*add contents of str1*/#"how r u??";
please tell me how to achieve this??

There are multiple answers possible. It depends a bit on how you want to figure out where to insert the text. One possibility is:
NSString *outStr = [NSString stringWithFormat:"%#%#%#", [str2 substringToIndex:?], str1, [str2 substringFromIndex:?]];

(Append always means add to the end. That's inserting a string in the middle.)
If you simply want to construct a literal string, use
#define STR1 #"Hello"
NSString* str2 = #"Hi..." STR1 #" how r u??";
To insert it in run time you need to convert str2 into a mutable string and call -insertString:atIndex:.
NSMutableString* mstr2 = [str2 mutableCopy];
[mstr2 insertString:str1 atIndex:4];
return [mstr2 autorelease];

Related

How to replace two different strings in iPhone programming

I am reading CSV file in my application.
I want to replace two different strings and print as one line. for example:
string1#$string2#$string3####string4
I want to replace #$ with , and #### with \n
and want to show the result on a UILabel.
You can use stringByReplacingOccurrencesOfString:withString: method, like this:
NSString *orig = "string1#$string2#$string3####string4";
NSString *res = [orig stringByReplacingOccurrencesOfString:#"#$" withString:#" "];
res = [res stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
Note that the original string does not get changed: instead, a new instance is produced with the replacements that you requested.
use
String = [String stringByReplacingOccurrencesOfString:#"#$" withString:#","];
String = [String stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
And then
yourLabel.text=String;
Try this
NSString *string = #"####abc#$de";
string = [string stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
string = [string stringByReplacingOccurrencesOfString:#"#$" withString:#","];
Hope it helps you

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

cut nsstring after a special character in iphone

I'm new in iPhone, I want to cut a string after a special character, lets take this example
"works.php" , I want to remove ".php" from this string meaning that I want to remove what's after "."
what is the code to do that ??
thanks in Advance.
It seems you want to use -stringByDeletingPathExtension.
[#"works.php" stringByDeletingPathExtension] // becomes #"works".
Note that this method deletes path extensions, which is not exactly the same as "remove what's after a dot". Please read the reference I've linked above for detail.
If you really just need to remove the string after the last dot, just use the conventional algorithm of (1) find the last dot (2) get a substring until the last dot:
NSString* input = #"works.php";
NSRange lastDotRange = [input rangeOfString:#"." options:NSBackwardsSearch];
if (lastDotRange.location != NSNotFound) {
return [input substringToIndex:lastDotRange.location];
} else {
return input;
}
For the specific example you can use:
- (NSString *)stringByDeletingPathExtension
The more general way to do what you want is:
NSString *originalString = #"works.php";
NSString *finalString =
[[originalString componentsSeparatedByString:#"."] objectAtIndex: 0];
You can replace the dot character with any character you want.
You can use:
myString = #"anything.php";
myString = [myString stringByReplacingOccurrencesOfString:#".php" withString:#""];
The advantage of this is that you can put anything to remove, not only a path. For example:
myString = #"anything-123";
myString = [myString stringByReplacingOccurrencesOfString:#"-123" withString:#""];
Another approach is NSRange and substringWithRange if you want other removing options..

iOS Beginner: String concatenation

I've concatenated string using the following code:
NSString *add = #"a ";
lbl.text = [add stringByAppendingString:lbl.text];
Which adds 'a ' to lbl variable every time I call the function.
But for some reason this method concatenates in a way that the new string adds in the beginning of what's already there, and not to the end.
Instead of getting AAABBB, I get BBBAAA. How do I fix this?
If a = AAA and b = BBB then you will need to write
[a stringByAppendingString:b];
So in your case it will be
[lbl.text stringByAppendingString:add]; as lbl.text = AAA and add = BBB.
For more information about this method please see NSString documentation.
NSMutableString *aString = [NSMutableString stringWithString:#"AAA"];
NSMutableString *bString = [NSMutableString stringWithString:#"BBB"];
[aString appendString:bString];
NSLog(#"Astring:%#",aString);
You can also use NSMutableString. Declare an NSMutableString *s, and then call [s appendString:..], or you can also call [s appendFormat:[NSString stringWithFormat:#"",];

How to replace a character in NSString without inserting a space?

Let's assume I have the string
NSString* myString = #"Hello,";
How can I remove the comma without leaving a space? I have tried:
NSString* newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
and
NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
But both are leaving spaces.
I just ran the following as a test
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
And I get 2010-04-05 18:51:18.885 TestString[6823:a0f] Helloxx as output. There is no space left.
NSString *newString = [myString substringToIndex:5];
That will ensure that there are only 5 characters in the string, but beware that this will also throw an exception if there are not at least 5 characters in the string to begin with. How are you handling this string? Is it being displayed to the user? Is it being written to a file? The code that you posted does not reproduce the error, so perhaps you should post the code that you are having a problem with.
the other way u can use.. by stringByReplacingCharactersInRange
token = [token stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"*"];
"token"is your want to replace the NSString and "i" is you want to change NSString by "*"