How to compare NSString having " sign? - iphone

I am trying to do like this, but it gives error as " is present.
if (!([post.videoID isEqualToString:#"<null>"])) {
How can I compare such string which contains " with it?

This is valid way to check null
if (!([post.videoID isEqual:[NSNull null]]))
{
}
And this is your solution...
if (!([post.videoID isEqualToString:#"\"<null>\""]))
{
}

Always remember to put \ as escape sequence when there is a " available in the code.
if (!([post.videoID isEqualToString:#"\"<null>\""])) {

You will have to check with [NSNull null] object.
if (!([post.videoID isEqual:[NSNull null]])) {
}

NSString *p=#"\"";
if ([p isEqualToString:#"\""]) {
NSLog(#"YES");
}

TRy this
NSString *str=[NSString stringWithFormat:#"\"%#\"",post.videoID ];
if (!([str isEqualToString:#"\"<null>\""])) {}

Related

Get the text in string variable

I m confused with this from many days and couldn't understand how to resolve it.
I get some data from web server and assigning it to string variable.In assigning it if sometimes no data is available then that string is updated to null(NULL) and to nil(nil) sometimes to (null).So I m confused how to compare data in that variable.
if(stringvariable==NULL) // couldnot understand how to compare here ,with NULL or nil or (null)
{
// do something
}
When will the string variable change its state (to NULL or nil or (null)) ?
use this code..
if([stringvariable isEqualToString:#""] || [stringvariable isEqual:nil])
{
//Data not Found
}
else{
// Data not nil
}
You can check like
if([str length]>0 || ![str isEqualToString:#""]) {
// String is not empty
}
It should be :
if(![stringvariable isEqualToString:#""])
{
// stringvariable is not Empty.
}
else
{
// stringvariable is Empty.
}

Remove first character from string if 0

I need to remove the first character from my UITextfield if it's a 0.
Unfortunately I don't know how to extract the value of the first character or extract the characters of the string after the first character.
Thanks
One solution could be:
if ([string hasPrefix:#"0"] && [string length] > 1) {
string = [string substringFromIndex:1];
}
You would probably want something like this, using hasPrefix:
if ([string hasPrefix:#"0"]) {
string = [string substringFromIndex:1];
}
You could also use characterAtIndex: which returns a unichar:
if ([string characterAtIndex:0] == '0') {
string = [string substringFromIndex:1];
}
Note that, 'a' is character, "a" is C string and #"a" is NSString. They all are different types.
Swift 3.0:
var myString = "Hello, World"
myString.remove(at: myString.startIndex)
myString // "ello, World"
Swift 3
hasPrefix("0") check if the first character is 0 in txtField
and remove it
if (self.txtField.text?.hasPrefix("0"))! {
self.txtField.text?.remove(at(self.txtField.text?.startIndex)!)
}
SWIFT 5.0
you can use the following sequence function to check whether string starts with "0" or not
if search.starts(with: "0") {
search.remove(at: search.startIndex)
print("After Trim search is",search)
} else {
print("First char is not 0")
}
Replace the string variable with your current variable name.
I hope this helps you
While loop to keep removing first character as long as it is zero
while (self.ammountTextField.text?.hasPrefix("0"))! {
self.ammountTextField.text?.remove(at: (self.ammountTextField.text?.startIndex)!)
}

Comparing text in UITextView?

How can we compare the text entered in UITextVIew with my default text in code to determine whether they are both the same or not?
You can use the methods of NSString for this.
1: isEqualToString: (case-sensitive)
if( [ myString isEqualToString: otherString ] )
{}
2: caseInsensitiveCompare: (case-insensitive)
if( [ myString caseInsensitiveCompare: otherString ] )
{}
3: localizedCaseInsensitiveCompare: (case-insensitive and localized)
if( [ myString localizedCaseInsensitiveCompare: otherString ] )
{}
I think this should work for you:
Though this is a case sensitive comparison of string
BOOL boolVal = [textView.text isEqualToString:#"My Default Text"];
Here is how you can do case insensitive comparison of string:
BOOL boolVal = [textView.text compare:#"My Default Text" options:NSCaseInsensitiveSearch]
Here if boolVal is YES then you can say that strings are same else they are different.
Hope this helps you.
First is to compare with the value abc and second is to compare with the String * str.
[textfield1.text isEqualToString:#"abc"]
or
[textfield1.text isEqualToString:str];
if([textfield.text isEqualToString:yourtext])
{
//true;
}
else
{
//false
}

How to compare self.title to a string in Objective C?

What am I doing wrong in the below code? My if statement is missing something:
if ([self.title] = "Upcoming Events") {
} else {
}
Correct would be:
if( [self.title isEqualToString:#"Upcoming Events"] )
self.title is a pointer, you can only compare it to other pointers using == not their values.
if ([self.title isEqualToString:#"Upcoming Events"]) {
NSLog(#"True");
} else {
NSLog(#"False");
}
You just have to write like this:
if([self.title isEqualToString:#"Upcoming Events"])
{
}
else
{
}
also .... in a if you should use "==" instead of "=". When there is "==" it is checking if they are equal while if there is "=" it gives the first one the value of the second.

How to know whether a UITextField contains a specific character

How would I say that if a UITextField has #"-" in it, do something.
Right now my code is like this. It doesn't seem to work:
if (MyUITextField.text == #"-") {
NSRange range = {0,1};
[a deleteCharactersInRange:range];
MyUITextField.text = MyUILabel.text;
}
I know that I am doing something very wrong with the code. Please help.
try changing == to [MyUITextField.text isEqualToString:#"-"]
as == tests to see if they are the same object, while isEqualToString compares the contents of the strings.
Assuming your string is defined as:
NSString *str = #"foo-bar";
To check if your string contains "-" you can do the following:
if ([str rangeOfString:#"-"].length > 0)
{
NSLog(#"Contains -");
}
It looks like you wanted to delete the first character if a string starts with a given character. In this case you can do something like this:
if ([str hasPrefix:#"f"])
{
NSLog(#"Starts with f");
}