I have some NSString object, for example:
NSString *inputString1 = #"Name is \"Mark\"";
NSString *inputString2 = #"Name is \"Joe\"";
I need to extract Mark and Joe values substrings from input.
Are there any solutions?
Thank you, in advance.
NSString *firstName =
[[inputString1 componentsSeparatedByString:#"\""] objectAtIndex:1];
NSString *name = [[inputString componentsSeparatedByString:#" "] lastObject];
name = [name stringByReplacingOccurencesOfString:#"\"" withString:#""];
Related
I currently tried to use this:
NSString *hello = #"Hello";
NSString *whatever = [hello stringByAppendingString:#", world!"];
it's possible to use this but it will make a lot of work because my goal is to do something like this:
NSString = "Hello" +variable+ "My name is" +variable + ",Good day"
You can use NSString's method stringWithFormat
example
NSString *str = [NSString stringWithFormat:#"Hello %# My Name is %# ",var1,var2]
or
NSString *str = [NSString stringWithFormat:#"Hello %# My Name is %# ",#"Stack Overflow",#"FreelancsAndroidLovesyou"]
You can do this alternatively, if both values are same then you can do this,
NSString *string = #"Hello YOUR_NAME, My name is YOUR_NAME, Good day";
string = [string stringByReplacingOccurrencesOfString:#"YOUR_NAME" withString:#"Your_desired_Name"];
UPDATE
If both values are different then you can do this,
NSString *string = #"Hello %#, My name is %#, Good day";
NSString *output = [NSString stringWithFormat:string, variable1, variable2];
You can use the NSString method stringWithFormat.
Objective C
NSString *strConcat = [NSString stringWithFormat:#"Hello %# My Name is %# ",var1,var2];
Swift
let strConcat = "Hello "+ strOne + " My Name is " + strTwo
OR
let strConcateSecond = "Hello \(strOne) My Name is \(strTwo)"
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:#" "]];
}
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];
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 have this string:
Test: String
And I want to select the text before the :, as if that were the result: "Test";
Any tip?
Thanks,
Alberto
You can split the string:
NSArray *words = [string componentsSeparatedByString:#":"];
Then select array's first element.
NSString *select = [words objectAtIndex:0];
NSLog(select);
NSString *firstWord = [[string componentsSeparatedByString:#":"] objectAtIndex:0];
you can use
NSString *str = #"Test: String";
NSSArray *Arrstr = [str componentsSeparatedByCharactersInSet:#":"];
Now you will have two strings in the array. Take first by :
NSString *str1 = [Arrstr objectatindex : 0];
Hope that helps you.