I have a logical condition that does not work. it would compare two characters, one taken from the xml and the other I insert myself.
to let you know I am attaching below the code
NSString *telefono = [NSString stringWithFormat:#"%#", [[arrayColonnine objectAtIndex:indexPath.row]objectForKey:#"telefono"]];
NSString *trimmedTelefono = [telefono stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
senzaTelefono = [[NSString alloc] initWithString:[trimmedTelefono stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if (telefono == #"NO")
if (trimmedTelefono == #"NO")
if (senzaTelefono == #"NO")
for each of the three strings if I do a log it print NO. but in none of the three cases the if works. how can I fix to make it work?
SOLUTION
the process is just this:
NSString *telefono = [NSString stringWithFormat:#"%#", [[arrayColonnine objectAtIndex:indexPath.row]objectForKey:#"telefono"]];
NSString *trimmedTelefono = [telefono stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedTelefono isEqualToString:#"NO"]) {
//do something...
}
You should use isEqualToString:
if ([telefono isEqualToString:#"NO"])
if (telefono == #"NO") compares the objects being the same, not the contents being the same.
Don't use == to compare NSString objects.
Related
I am writing code in objective-c. I would like to extract a url from a string.
Here is my code:
NSMutableString *oneContent = [[latestPosts objectAtIndex:i] objectForKey:#"content"];
NSLog(#"%#", oneContent);//no problem
NSString *string = #"http";
if ([oneContent rangeOfString:string].location == NSNotFound) {
NSLog(#"string does not contain substring");
} else {
NSLog(#"string contains substring!");
}
As you can see, I want to extract a url from the oneContent string, and I have checked that oneContent definitely contains "http", but why does the result show nothing?
Is there some better way to extract the url?
Check oneContent or the actual code you are running.
This works:
NSMutableString *oneContent = [#"asdhttpqwe" mutableCopy];
NSLog(#"%#", oneContent);//no problem
NSString *string = #"http";
if ([oneContent rangeOfString:string].location == NSNotFound) {
NSLog(#"string does not contain substring");
} else {
NSLog(#"string contains substring!");
}
NSLog output:
Untitled[5911:707] asdhttpqwe
Untitled[5911:707] string contains substring!
It is probably best not to use a Mutable string unless there is some substantial reason to do so.
I would suggest using NSScanner.
I am concatenating two string using following way I want space between Added Content To My Learning And the second String any idea how to give space thanks
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [firstString stringByAppendingString:secondString];
You can do it like this.
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [NSString stringWithFormat:#"%# %#", firstString, secondString];
Did you try?
NSString *firstString =#"Added Contnet To My Learning ";
// a space after the first string
Here is all way you can do with string
//1st Way
NSString *finalString = [NSString stringWithFormat:#"%# %#",firstString,secondString];
//2nd Way
NSString *finalString = [firstString stringByAppendingFormat:#" %#",secondString];
//3rd way
NSArray *ary= [NSArray arrayWithObjects:firstString,secondString, nil];
NSString *finalString= [ary componentsJoinedByString:#" "];
Follow this how to concatenate two strings in iphone
This question is for manipulating NSString in xcode.
I have a XML text string that I get from the web that looks like this
<current temperature="73" day="Mon" humidity="59" windspeed="10"></current>
How can I get individual values from this string and put them in my NSString variables?
e.g.
NSString *tempStr = ??
NSString *dayStr = ??
NSString *windspeedStr = ??
First, download and include RaptureXML within your project as described on the RaptureXML project site.
For parsing the single given line, use the following snippet - your input is passed as inXmlString;
//transform string into an XML DOM
RXMLElement *rootNode = [RXMLElement elementFromXMLString:inXmlString
withEncoding:NSUTF8StringEncoding];
if (rootNode == nil || ![rootNode isValid])
{
//do something, we failed!
}
else
{
NSString *temperature = [rootNode attribute:#"temperature"];
NSString *day = [rootNode attribute:#"day"];
NSString *windspeed = [rootNode attribute:#"windspeed"];
}
The basic idea is to use the NSString method componentsSeparatedByString: to parse out the data you want. You'll probably need to do a bit more work to get it exactly right for your scenario.
NSArray* paArray1= [pstrXMLString componentsSeparatedByString:#" "];
temlStr= [[[paArray1 objectAtIndex:1] componentsSeparatedByString:#"="] objectAtIndex:1];
dayStr= [[[paArray1 objectAtIndex:2] componentsSeparatedByString:#"="] objectAtIndex:1];
windspeedStr= [[[paArray1 objectAtIndex:3] componentsSeparatedByString:#"="] objectAtIndex:1];
Consider this code:
NSString *aString = #"\tThis is a sample string";
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(#"The trimmed string: %#",trimmedString);
trimmedString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
NSLog(#"The trimmed string: %#",trimmedString);
Here if I use characterSetWithCharactersInString: on the same NSString object trimmedString, my previous whitespace trimming effect gets removed..
My question is,
Is there any possibility to use more than one NSCharacterSet object to the same NSString???
or Suggest me some other way to do this please, but the NSString object should be one and the same..
The problem is not because of character sets. Its because you are using aString while trimming the string second time. You should use trimmedString instead. Your code should look like,
trimmedString = [trimmedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"string"]];
What about this:
NSString *aString = #"\tThis is a sample string";
NSMutableCharacterSet *customSet = [[NSMutableCharacterSet alloc] init];
[customSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[customSet addCharactersInString:#"string"];
NSString *trimmedString = [aString stringByTrimmingCharactersInSet:customSet];
[customSet release];
Hi all i want extract the last part from string which is a four digit number '03276' i:e http://www.abc.com/news/read/welcome-new-gig/03276
how can i do that.
You can also use
NSString *sub = [#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent];
If you know how many characters you need, you can do something like this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [string substringFromIndex:[string length] - 5];
If you just know that it's the part after the last slash, you can do this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [[string componentsSeparatedByString:#"/"] lastObject];
Since *nix uses the same path separators as URL's this will be valid as well.
[#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]
If you know the length of the number, and it's not gonna change, it can be as easy as:
NSString *result = [string substringFromIndex:[string length] - 4];
If the last part of the string is always the same length (5 characters) you could use this method to extract the last part:
- (NSString *)substringFromIndex:(NSUInteger)anIndex
Use the length of the string to determine the start index.
Something like this:
NSString *inputStr = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *newStr = [inputStr substringFromIndex:[inputStr length]-5];
NSLog(#"These are the last five characters of the string: %#", newStr);
(Code not tested)
NSString *str = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSArray *arr = [str componentSeparatedBy:#"gig/"];
NSString *strSubStringDigNum = [arr objectAtIndex:1];
strSubStringDigNum will have the value 03276
Try this:
NSString *myUrl = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *number = [[myUrl componentsSeparatedByString:#"/"] objectAtIndex: 5];