stringByReplacingOccurrencesOfString method call does not change the string I call it on - ios

Inside of a for loop, I have an if statement that looks like this:
if([numberValues rangeOfString:#"+"].location == NSNotFound) {
NSLog(#"Phone Number does not contain a plus sign.");
} else {
NSLog(#"Phone number does contain a +");
[numberValues stringByReplacingOccurrencesOfString:#"+" withString:#""];
NSLog(#"This new numberValues object should not have a + anymore: %#", numberValues);
}
I can confirm that the rangeOfString: method call works perfectly because there are phone numbers in my address book that do not start with a "+" and it will print "Phone Number does not contain a plus sign." to the log.
My problem is that the stringByReplacingOccurrencesOfString: call does not work. Right after the call, when I print the numberValues object data to the log, the phone number still looks like this "+13997573173" instead of being printed without the plus sign like "13997573173".
I have even tried changing the "+" in the method call to something like the first 4 digits of the phone number and it still will not replace the target string with the new string.
It just prints the same value every time. Any ideas why stringByReplacingOccurrencesOfString: is not working for me?

The method stringByReplacingOccurrencesOfString:withString: actually returns a string, and does not mutate the string you've passed in.
So you likely want to do something like this instead:
numberValues = [numberValues stringByReplacingOccurrencesOfString:#"+"
withString:#""];

numberValues = [numberValues stringByReplacingOccurrencesOfString:#"+"
withString:#""];
stringByReplacingOccurrencesOfString return a new string.assign it to origin string.

Related

Remove BackEnd character in a 'NSString'

I'm facing a problem when I try to remove a character in a 'NSString'. The character is a backend (\n).
My 'NSString' is for example like this :
My text is
also in a second line
And I want to get all in one line like this :
My text is also in a second line
The problem is I don't know how to change this...
I tried to locate the '\n' characters with a loop :
for (int delete = 0; delete < myString.length; delete++)
{
if ([myString characterAtIndex:delete] == 10)
{
[myString stringByReplacingCharactersInRange:NSMakeRange(delete,0) withString:#" "];
}
}
Or things like :
myString = [myString stringByReplacingOccurrencesOfString:#"\r" withString:#" "];
(I see that \r could be the backend in a nslog...)
Nothings work..
Thank you for your help in advance !
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
is correct.
If it doesn't work, then the assumption that there is a combination of "\" and "n" characters is wrong.
Do not use NSLog. NSLog already applies carriage returns to the string. Instead put a breakpoint on the line where we call stringByReplacing... and then hover over the myString. Wait a second or two and you will see the "original unformatted content"...this way you can check what you are really trying to replace..

How to check a string using AND or OR logic with NSCharacterSet

I am using Zxing library to scan the Barcodes. The result is stored in an NSString. Here I am looking into two cases:
Case:'semicolon' : If the result string contains semicolon character....then store it in Semicolon Array
myWords_semicolon = [_myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#";,;;"]
];
//here myWords_semicolon is a NSArray
Case: 'pipe' : If the result string contains pipe character...then store it in a pipe array.
myWords_pipe = [_myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"|,||"]
];
What I tried to do is, if the result string contains semicolon......go to case :'semicolon' ......if the result contains pipe go to case: 'pipe'. I used this to do that but couldn't get the right solution.
if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#";,;;"]].location != NSNotFound) {
NSLog(#"This string doesnt contain semicolon characters");
myWords=myWords_pipe;
}
if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:#"|,||"]].location != NSNotFound) {
NSLog(#"This string doesnt contain pipe characters");
myWords=myWords_semicolon;
}
Here in this case....only semicolon is case is working,even though I scan pipe case ,the scanner is unable to recognize the pipe case.. Is there any other way to use && or || logic here?
The problem with your code is that both sets contain commas, and at the same time both strings with pipes and strings with semicolons contain commas. Therefore, only the assignment from the last of the two ifs is going to have an effect, because both ifs will "fire".
You should be able to fix this by removing commas and duplicate pipes from your sets. Moreover, you should be able to simplify it further by using rangeOfString: method instead of rangeOfCharacterFromSet:
if ([_myString rangeOfString:#";"].location != NSNotFound) {
NSLog(#"This string contains a semicolon");
myWords=myWords_semicolon;
}
if ([_myString rangeOfString:#"|"].location != NSNotFound) {
NSLog(#"This string contains a pipe");
myWords=myWords_pipe;
}

objective c check string suffix from character set

I'm trying to check the suffix of a string against a character set but I'm getting errors:
if ([string hasSuffix:[NSCharacterSet characterSetWithCharactersInString:#"(+-*/"])
What am I doing wrong, is there a correct alternative?
-[NSString hasSuffix:] takes an NSString as its argument, not an NSCharacterSet. Your best bet is probably to call -[NSString rangeOfCharacterFromSet:options:] with NSBackwardsSearch as an option and check that the location is at the end of the string.
What are you trying to do?
I think that you are trying to see if the first letter of your string is one of the characters from the set. If so, then use this:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"(+-*/"];
if ([[string substringToIndex:1] rangeOfCharacterFromSet:set].location != NSNotFound)
// String starts with one of the characters from the character set

How to Search a List of Names in iOS?

So basically I want to use a UISearchBar to search an Array of Names.
Inside the Array the Names have this Format:
[FIRST NAME] ([SECOND NAME]) [LAST NAME]
Using
if (![string rangeOfString:seachTextPart].location == NSNotFound)
I have managed to search Names entering ONLY the FIRST NAME into the UISearchBar.
But as soon as I enter a SPACE i get NO RESULTS.
Also I want to be able to search by entering just the LAST NAME.
Can anyone please help me?
Thanks
Assuming your format is follow:name = #"[John]([James])[Smith]";
Searching [name rangeOfString:#"John"] would yield found.
Searching [name rangeOfString:#"John "] would yield not found, since #"John " is not in name
But in this case, [name rangeOfString:#"Smith"] would yield found.
Try this: Remove all spaces and lower case strings
searchTerm = [[searchTerm stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
NSRange searchName = [[name lowercaseString] rangeOfString:searchTerm];
If you want to test, just simply do this"
if (searchName == 0) //it does not match
Byte's code works unless they type "John Smith." If you want something more robust you can use - (NSArray *)componentsSeparatedByString:(NSString *)separator and then search for any of the strings in the Array. So Typing "John Smith" would return all of the Johns and all of the Smiths
here:
NSArray *theTerms = [searchTerm componentsSeparatedByString:#" "];
then run a for loop through all of theTerms and check them against your names adding the ones that contain the search terms to an array and then displaying them.

Objective-C - Remove last character from string

In Objective-C for iOS, how would I remove the last character of a string using a button action?
In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:
if ([string length] > 0) {
string = [string substringToIndex:[string length] - 1];
} else {
//no characters to delete... attempting to do so will result in a crash
}
If you want a fancy way of doing this in just one line of code you could write it as:
string = [string substringToIndex:string.length-(string.length>0)];
*Explanation of fancy one-line code snippet:
If there is a character to delete (i.e. the length of the string is greater than 0)
     (string.length>0) returns 1 thus making the code return:
          string = [string substringToIndex:string.length-1];
If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
     (string.length>0) returns 0 thus making the code return:
          string = [string substringToIndex:string.length-0];
     Which prevents crashes.
If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:
[myString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];
The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.
In fact, the iOS header file which contains the declaration of substringToIndex contains the following comment:
Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters
See how to use rangeOfComposedCharacterSequenceAtIndex: to delete the last character correctly.
The documentation is your friend, NSString supports a call substringWithRange that can shorten the string that you have an return the shortened String. You cannot modify an instance of NSString it is immutable. If you have an NSMutableString is has a method called deleteCharactersInRange that can modify the string in place
...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...

Resources