regex for adding a space between two words if not already there - objective-c

I would like to add a spacing between two words if its already not there:
//Sample 1
NSString *word1 = #"First";
NSString *word2 = #"Word";
NSString *output = [NSString stringWithFormat:#"%#%#", word1, word2];
//output = FirstWord --> I want "First Word"
If there is already a space "First " then it should not add another one.

Just trim the first string, and then always put a space.
NSString *word1 = #"First";
NSString *word2 = #"Word";
NSString *word1Trimmed = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
NSString *output = [NSString stringWithFormat:#"%# %#", word1Trimmed, word2];

Related

Include a variable in the middle of NSString

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)"

How to remove the first space from the NSString?

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:#" "]];
}

Replace each word in a string with (the first letter + number of characters between + last letter)

I'm using objective-c to parse a sentence here:
NSString *myString = #“Some words to form a string”;
Here is what I have so far:
NSMutableString *firstCharacters = [NSMutableString string];
NSMutableString *lastCharacters = [myString substringFromIndex:[myString length] - 1]
NSArray *arrayOfWords = [myString componentsSeparatedByString:[NSCharacterSet whitespaceCharacterSet]];
for (NSString *word in arrayOfWords) {
if ([word length] > 0) {
NSString *firstLetter = [word substringToIndex:1];
[firstCharacters appendString:lastCharacters];
and then I am really stumped at this point. I want to NSLog the recombined string so that it looks like this:
"S2e w3s to f2m a s3g"
Please try following code :
NSString *myString = #"Some words to form a string";
NSArray *wordsInSentence = [myString componentsSeparatedByString:#" "];
NSMutableArray *expectedResultArray = [[NSMutableArray alloc] init];
for (NSString *word in wordsInSentence) {
NSString *finalExpectedString = word;
if (word.length > 2) {
NSString *firstLetterInWord = [word substringToIndex:1];
NSString *lastLetterInWord = [word substringFromIndex:[word length] - 1];
finalExpectedString = [NSString stringWithFormat:#"%#%d%#", firstLetterInWord, (int)word.length - 2, lastLetterInWord];
}
[expectedResultArray addObject:finalExpectedString];
}
NSString *printString = [expectedResultArray componentsJoinedByString:#" "];
NSLog(#"Result : %#", printString);

Remove prefix from NSString

How can I delete the prefix "test" from a NSString?
I've tried stringByReplacingOccurrencesOfString: but it's not what I want because it's the prefix that I want to remove not from the other occurrences of the string.
NSString *prefixToRemove = #"test";
NSString *newString = [originalString copy];
if ([originalString hasPrefix:prefixToRemove])
newString = [originalString substringFromIndex:[prefixToRemove length]];

stringwithformat issue in string searching

I did following experiment. Can any one point out why strings initialized with stringwithformat fail in string searching?
NSString *test1 = #"Hello";
NSString *test2 = #"Hello";
NSString *test3 = [NSString stringWithFormat:#"%# ", test2];
NSRange titleResultsRange = [test1 rangeOfString:test2 options:NSCaseInsensitiveSearch];
I get titleResultsRange.length > 0
But when I do -
NSRange titleResultsRange = [test1 rangeOfString:test3 options:NSCaseInsensitiveSearch];
I get titleResultsRange.length = 0
Why?
Could it be that test3 is "Hello " not "Hello".
NSString *test1 = #"Hello";
NSString *test2 = #"Hello";
NSString *test3 = [NSString stringWithFormat:#"%#", test2];
NSRange titleResultsRange = [test1 rangeOfString:test2 options:NSCaseInsensitiveSearch];
Try now. In your code test3 string contain extra white space.