01-12-2011, 03:14 PM
#1 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
Comparing NSString
Hi
i have tried all my best but just cant get to a solution
I would like to compare incoming socket against i.e @"ack"
here is the code
the incoming string:
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
compare to:
NSString *str1 = @"
ack ";
comparison code:
if ([str isEqualToString:str1]) {
label.text = str;
}
when I nslog(str,nil) it shows that it equals ack
but when i compare it to ack it doesn't matchhhh
any ideas please
01-12-2011, 04:07 PM
#2 (permalink )
Reading the Documentation
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
There's no need to post the same question multiple times.
Are they exact in regards to case?
01-12-2011, 04:12 PM
#3 (permalink )
Reading the Documentation
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
Just ran this and it worked ok:
Code:
NSString *str1 = @"ack";
NSData *data = [str1 dataUsingEncoding:NSASCIIStringEncoding];
NSString *str2 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
if ([str2 isEqualToString:@"ack"]) {
NSLog(@"It's equal");
}
[str2 release];
01-13-2011, 02:53 AM
#4 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
Thanks mate, but i tried it already and not working
the strange thing is that if i convert str to integer it works
as highlighted below
here is all the code
Code:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if (data == nil) {
data = [[NSMutableData alloc] init];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
[data appendBytes:(const void *)buf length:len];
int bytesRead;
bytesRead += len;
} else {
NSLog(@"No data.");
}
NSString *str = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
NSLog(str,nil);
if ([str isEqualToString:@"ack"]) {
NSLog(@"It's equal");
label.text = str;
}
else {
label.text = @"";
txtMessage.backgroundColor = [UIColor clearColor];
txtMessage.text = str;
}
This below works by the way
int x = [str intValue];
if (x > 30)
{
//txtMessage.text = @"High";
label.text = @"High temperature";
txtMessage.backgroundColor = [UIColor redColor];
txtMessage.text = str;
}
[str release];
[data release];
data = nil;
} break;
}
}
01-13-2011, 03:05 AM
#5 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 159
What's the output of NSString *str = [[NSString alloc] initWithData:data
If you do NSLog(@"output = %@", str);
__________________
Aaron
01-13-2011, 04:42 AM
#6 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
2011-01-13 21:42:17.765 Socket[57886:207] output = ack
01-13-2011, 05:51 AM
#7 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
strange, isEqualToString should solve problems about encoding, are you sure that there isn't strange characters? maybe a space, or something other in str, maybe terminator character \0?
__________________
Last edited by dany_dev; 01-13-2011 at 05:56 AM .
01-13-2011, 06:02 AM
#8 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
Like Dany88 said, check for non-visible characters. What is the output of
Code:
NSLog(@"output = %@ and length = %d", str, str.length);
We know the length of @"ack" should be 3.
01-13-2011, 06:03 AM
#9 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
It is possible that the server is sending ack with the /n
Not too sure
If it is, what should I try
@"ack/n" ??
01-13-2011, 06:08 AM
#10 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
2011-01-13 23:05:46.633 Socket[58689:207] output = ack
and length = 4
Thanks for your help
does it mean there is a terminating character \0
any ideas how to compare against that.
01-13-2011, 06:17 AM
#11 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
Quote:
Originally Posted by
ossama
2011-01-13 23:05:46.633 Socket[58689:207] output = ack
and length = 4
Thanks for your help
does it mean there is a terminating character \0
any ideas how to compare against that.
Did you post "and length = 4" on a separate line deliberately, or did the newline just appear? It looks like there is a newline character in your data. That makes sense if it came from a text file.
01-13-2011, 06:23 AM
#12 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
str = [str stringByReplacingOccurancesOfString:@"\n" withString:@""];
__________________
01-13-2011, 07:29 AM
#13 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
no, the new line just appeared. not deliberate which means there is a carriage return
I tried
Code:
str = [str stringByReplacingOccurancesOfString:@"\n" withString:@""];
but i get an error and the app quits after trying to connect to server
Quote:
warning: 'NSString' may not respond to '-stringByReplacingOccurancesOfString:withString:'
01-13-2011, 07:34 AM
#14 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
this seems to have fixed the problem I believe, I will test this later, getting very late now
Code:
if ([str isEqualToString:@"ack\n"])
01-13-2011, 07:43 AM
#15 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 20
Thanks for your time and help.
if you ever need help, yell at me
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 367
9 members and 358 guests
7twenty7 , blueorb , iAppDeveloper , iGamesDev , Mah6447 , Morrisone , mottdog , sacha1996 , Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one