Is there a way to concatenate two strings and inject a character. I have :
NSString *firstString = #"http://www.stackoverflow.com";
NSString *secondString = #"supercooloptions";
NSString *result = [firstString stringByAppendingString: secondString];
NSLog(#"the final string with "/" injection = %#", result);
I'd like the final string with "/" injected = www.stackoverflow.com/supercooloptions
You can use a different method - for example, stringWithFormat:
NSString *result = [NSString stringWithFormat:#"%#/%#", firstString, secondString];
For this specific case, use stringByAppendingPathComponent:
NSString *result = [firstString stringByAppendingPathComponent:secondString];
Related
I want to remove only first space in below string.
NSString *str = #"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
Note: There is a space after IF_Distance and another space after
GET_Mi. I am unable to remove the space after IF_Distance.
Use rangeOfString: to locate the first space, then use stringByReplacingCharactersInRange:withString: to replace it with the empty string.
Remove space by using below code.
NSString *str = #"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
NSString *secondString = [str stringByReplacingOccurrencesOfString:#"IF_Distance " withString:#"IF_Distance"];
Try This:
NSString *str = #"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
NSString *firstStringContainingSpace = [[str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject];//firstStringContainingSpace = IF_Distance
str = [str stringByReplacingCharactersInRange:[str rangeOfString:[NSString stringWithFormat:#"%# ",firstStringContainingSpace]] withString:firstStringContainingSpace];
Output:
str = #"IF_Distance(GET_Mi mi=km*1.4,STRING1,STRING2)";
You can remove first space by using following code:
First find space by using rangeOfString: and then remove by using stringByReplacingCharactersInRange:withString: method.
Like,
NSString *str = #"IF_Distance (GET_Mi mi=km*1.4,STRING1,STRING2)";
NSString *strSpace = #" ";
NSRange range = [str rangeOfString:strSpace];
NSString *strFinal;
if (NSNotFound != range.location) {
strFinal = [str stringByReplacingCharactersInRange:range withString:#""];
}
If you are looking for some more universal way - this is the variant of it:
- (NSString *)removeWhitespaces:(NSString *)string {
NSMutableArray * stringComponents = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy];
NSString * fStringComponent = [stringComponents firstObject];
[stringComponents removeObject:fStringComponent];
return [fStringComponent stringByAppendingString:[stringComponents componentsJoinedByString:#" "]];
}
This question already has answers here:
How to take NSString after specific character which occurs multiple time in NSString?
(3 answers)
Closed 8 years ago.
I have an NSString which represents paths like
.../App/Some/Directory/myClass.mm
Now I want to trim
.../App/Some/Directory/
to get only the Class' name/file. How to achieve this?
You can convert your string to a NSURL, and the get the lastPathComponent to get the "myClass.mm" you want.
NSString* filePath = #"/App/Some/Directory/myClass.mm";
NSURL *url = [NSURL URLWithString:filePath];
NSString* lastComponent = [url lastPathComponent];
Should do the trick.
You can separate the string into components (using the / as the separator), and then the filename will be the last element of the output array.
NSString *input = #".../App/Some/Directory/myClass.mm";
NSArray *array = [input componentsSeparatedByString:#"/"];
NSLog( #"%#", [array lastObject] );
NSString class has lastPathComponent method, also you can solve your problem like this:
NSString *path;
NSString *trimmed;
NSRange range = [path rangeOfString:#"/" options:NSBackwardsSearch];
if (range.location != NSNotFound)
trimmed = [path substringFromIndex:range.location+1];
else
trimmed = path;
NSString* str = #"/App/Some/Directory/myClass.mm";
NSRange replaceRange = [str rangeOfString:#"/myClass.mm"];
if (replaceRange.location != NSNotFound){
NSString* result = [str stringByReplacingCharactersInRange:replaceRange withString:#""];
NSLog(#"Result :%#",result);
}
I'm appending a string to a url which then inputs into a database, it works with a single worded string e.g
NSString * string = #"one";
however if my string has two words such as
NSString * string = #"one two";
it does not work. Please see the code below.
NSMutableString * urlString = [NSMutableString stringWithString:url];
[urlString appendString:[NSString stringWithFormat:#"?%#=%#",kWord,word]];
The problem obviously lies with stringWithFormat;
turns out the problem is with the space.
Space characters (and certain others) are not allowed in URLs. You need to convert the space to %20. Here is the proper solution:
NSString *url = #"http://example.com/process.php";
NSString *kword = #"param";
NSString *word = #"one two";
NSMutableString * urlString = [url mutableCopy];
[urlString appendFormat:#"?%#=%#", kWord, [word stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
I have tried couple exampes here, hope it helps.
NSString *string1 = #"String1";
NSString *string2 = #"String2 String3";
NSMutableString *appendingString=[NSMutableString stringWithString: [string1 stringByAppendingString:string2]];
NSLog(#"String1:%# String2:%#",string1,string2);
NSLog(#"StringwithAppend:%#",appendingString);
//------
NSString *url=#"www.abc.com";
NSMutableString * urlString = [NSMutableString stringWithString:url];
NSLog(#"URL String before append:%#",urlString);
NSString *kWord=#"key";
NSString *word=#"word";
[urlString appendString:[NSString stringWithFormat:#"?%#=%#",kWord,word]];
NSLog(#"URL String after append:%#",urlString);
Console Log:
String1:String1 String2:String2 String3
StringwithAppend:String1String2 String3
URL String before append:www.abc.com
URL String after append:www.abc.com?key=word
Try it this way.
NSString * urlString= [NSString stringWithFormat:#"%#?%#=%#", url, kword, word];
I want to place a string within a string. Basically in pseudo code:
"first part of string" + "(varying string)" + "third part of string"
How can I do this in objective-c? Is there a way to easily concatenate in obj-c? Thanks!
Yes, do
NSString *str = [NSString stringWithFormat: #"first part %# second part", varyingString];
For concatenation you can use stringByAppendingString
NSString *str = #"hello ";
str = [str stringByAppendingString:#"world"]; //str is now "hello world"
For multiple strings
NSString *varyingString1 = #"hello";
NSString *varyingString2 = #"world";
NSString *str = [NSString stringWithFormat: #"%# %#", varyingString1, varyingString2];
//str is now "hello world"
Variations on a theme:
NSString *varying = #"whatever it is";
NSString *final = [NSString stringWithFormat:#"first part %# third part", varying];
NSString *varying = #"whatever it is";
NSString *final = [[#"first part" stringByAppendingString:varying] stringByAppendingString:#"second part"];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendFormat:#"%# third part", varying];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendString:varying];
[final appendString:#"third part"];
NSString * varyingString = ...;
NSString * cat = [NSString stringWithFormat:#"%s%#%#",
"first part of string",
varyingString,
#"third part of string"];
or simply -[NSString stringByAppendingString:]
You would normally use -stringWithFormat here.
NSString *myString = [NSString stringWithFormat:#"%#%#%#", #"some text", stringVariable, #"some more text"];
Just do
NSString* newString=[NSString stringWithFormat:#"first part of string (%#) third part of string", #"foo"];
This gives you
#"first part of string (foo) third part of string"
Iam amazed that none of the top answers pointed out that under recent Objective-C versions (after they added literals), you can concatenate just like this:
#"first" #"second"
And it will result in:
#"firstsecond"
You can not use it with NSString objects, only with literals, but it can be useful in some cases.
simple one:
[[#"first" stringByAppendingString:#"second"] stringByAppendingString:#"third"];
if you have many STRINGS to Concatenate, you should use NSMutableString for better performance
I'm a java guy coming over to Objective-C. In java, to add a variable to a string you'd have to do something along the lines of:
someString = "This string is equal to " + someNumber + ".";
I can't figure out how to do it in Objective-C though. I have an NSMutableString that I'd like to add to the middle of a string. How do I go about doing this?
I've tried:
NSString *someText = #"Lorem ipsum " + someMutableString;
NSString *someText = #"Lorem ipsum " + [someMutableString stringForm];
and a few other things, none of which seem to work. Also interchanged the +s with ,s.
You can use appendString:, but in general, I prefer:
NSString *someText = [NSString stringWithFormat: #"Lorem ipsum %#", someMutableString];
NSString *someString = [NSString stringWithFormat: #"This is string is equal to %d.", someInt];
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %#.", someNSNumber];
or, alternatively:
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %d.", [someNSNumber intValue]];
etc...
These strings are autoreleased, so take care not to lose their value. If necessary, retain or copy them and release them yourself later.
Try this:
NSMutableString * string1 = [[NSMutableString alloc] initWithString:#"this is my string"];
[string1 appendString:#" with more strings attached"];
//release when done
[string1 release];
You need to use stringByAppendingString
NSString* string = [[NSString alloc] initWithString:#"some string"];
string = [string stringByAppendingString:#" Sweet!"];
Don't forget to [string release]; when your done of course.
NSMutableString *string = [[NSMutableString alloc] init];
[string appendFormat:#"more text %#", object ];