2

我有两个完全相同的字符串,但它们没有被比较为相等。一个字符串是可变字符串,另一个是普通字符串,我有一个 if 语句来检查两个字符串是否相等。两个字符串的输出是这样的:

2013-04-08 09:09:31.555 Pin2Own[650:1303] 6447 E Crocus Dr
2013-04-08 09:09:31.555 Pin2Own[650:1303] 6447 E Crocus Dr

顶部是可变字符串,底部是普通字符串。我尝试将可变字符串转换为普通字符串,但我遇到了同样的问题。我从 NSXMLParser 子类中的 xml 文档中获取可变字符串,另一个从单例中的可变数组中获取。这是 NSXMLParser 类的代码:

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    address = nil;
    zpid = nil;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"street"])
    address = [[NSMutableString alloc] init];

    if ([elementName isEqualToString:@"zpid"])
    zpid = [[NSMutableString alloc] init];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(address)
    [address setString:string];

    else if(zpid)
    [zpid setString:string];
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"street"])
    NSLog(@"%@", address);

    if ([elementName isEqualToString:@"zpid"])
    NSLog(@"zip id is: %@", zpid);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{    
    NSMutableArray *annotations = [[Data singleton] annotations];

    for (MapPoint *mp in annotations)
    {
        NSLog(@"%@", mp.fullAddress);
        if ([mp.fullAddress isEqualToString:address])
        {
            mp.zpid = zpid;
        }
        else
        {
            NSLog(@"Did not match address");
        }
    }
}

所以我正在比较两个地址,如果地址相同,则设置一个属性,我确信这将永远是正确的。有没有人看看有什么问题?我真的不知道为什么会这样。

4

4 回答 4

6

在比较它们之前尝试从字符串中修剪空白字符。您可以使用以下

firstString = [firstString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
secondString = [secondString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
BOOL stringsAreEqual = [firstString isEqualToString:secondString];
于 2013-04-08T16:32:11.167 回答
3

不要责怪可靠的isEqualToString:方法。

您必须在任何地址字符串的前导或尾随添加一些空格。

用于stringByTrimmingCharactersInSet:删除前导和尾随空格。

于 2013-04-08T16:22:04.750 回答
1

清理字符串是一门艺术,而不是一门科学,但在空格的情况下,它可以做得相当好:

// Various types of whitespace in the input string
NSString *dirtyStr = @"   \tJoe\t Doe \n 42,    Main St. \n\rMetropolis    12345  ";

// First trim the initial and final white space characters away
NSString *trimmed  = [dirtyStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"Trimmed: \"%@\"", trimmed);

// Then remove repeated space characters in the middle (if consistent with your requirements)
NSString *cleaned  = [trimmed stringByReplacingOccurrencesOfString:@"\\s+" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, trimmed.length)];

NSLog(@"Cleaned: \"%@\"", cleaned);
于 2013-04-08T16:36:56.797 回答
0

您的字符串是否包含不可见的控制字符?

string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]] componentsJoinedByString:@""]
于 2021-01-20T07:45:06.003 回答