我有一个文本字符串,我想检查它的几个条件。 我在 C# 中有这个函数,我想将它移植到 Objective C (我是新手) 如果检查函数包含 (1) 中的任何一个且不包含 (2) 中的任何一个,则应返回 true
(1) 包含以下任意内容:
keyword1
或者
keyword2 AND keyword3
或者
keyword4
(2) 不应包含任何这些关键字
keyword4
或者
keyword5
或者
keyword6
我使用 LINQ 在 C# 中完成了此函数:
public static string ProcessText(string text, string postiveKeywords, string negativeKeywords)
{
//prepare the strings to lists
var simpleTextKeywords = text.Split(' ');
//trim any other characters
for (int i = 0; i < simpleTextKeywords.Length; i++ )
{
simpleTextKeywords[i] = simpleTextKeywords[i].Trim(
'~','!','@','#','$','%','^','&','*','(',')','_','+','-','=','[',']',';','\'','\\',',','.','/','{','}',':','"','|','<','>','?');
}
string [] listOfKeywords;
if ( !string.IsNullOrWhiteSpace(postiveKeywords))
listOfKeywords = postiveKeywords.Split(',');
else
listOfKeywords = new string[] { };
string[] listOfnegativeKeywords;
if ( !string.IsNullOrWhiteSpace(negativeKeywords ))
listOfnegativeKeywords = negativeKeywords.Split(',');
else
listOfnegativeKeywords = new string[] { };
//parse the keywordlist
var matches = listOfKeywords
.Any(OR => OR.Split('&').All(kw => simpleTextKeywords.Contains(kw)
&& !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
&& !string.IsNullOrWhiteSpace(kw)));
if (!matches)
{
return string.Empty;
}
//return the first match
return listOfKeywords
.First(OR => OR.Split('&')
.All(kw => simpleTextKeywords.Contains(kw)
&& !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
&& !string.IsNullOrWhiteSpace(kw)));
}
更新: 以下是到目前为止的代码:
+ (bool )ProcessText:(NSString*)text
andPositiveKeyword:(NSString*)positiveKeywords
andNegativeKeyword:(NSString*)negativeKeywords
{
//split the text words
NSMutableArray* simpleTextKeywords = [(NSArray*)[text componentsSeparatedByString:@" "] mutableCopy];
//trim unwanted characters
for (int i=0; i< simpleTextKeywords.count; i++) {
simpleTextKeywords[i] = [simpleTextKeywords[i] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?"]];
}
NSArray* listOfPositiveKeywords = [[NSArray alloc] init];
if ( ![NSString stringIsNilOrEmpty:positiveKeywords])
{
listOfPositiveKeywords = [positiveKeywords componentsSeparatedByString:@","];
}
NSArray* listOfNegativeKeywords = [[NSArray alloc] init];
if ( ![NSString stringIsNilOrEmpty:negativeKeywords])
{
listOfNegativeKeywords = [ negativeKeywords componentsSeparatedByString:@","];
}
BOOL matches = false;
for (int i=0; i< listOfPositiveKeywords.count; i++) {
//if the keyword has "&"
//all matches should be true
//split the &
NSArray* andKeywords = [listOfPositiveKeywords[i] componentsSeparatedByString:@"&"];
matches = true;
for (int j=0; j<andKeywords.count; j++) {
if ( ![simpleTextKeywords containsObject:andKeywords[j]] )
{
matches = false;
break;
}
}
if ( matches == true )
break;
}
//if there are any matches we have to check if it doesnt have any negative keyword
if ( matches)
{
for ( int i =0; i < listOfNegativeKeywords.count;i++)
{
if ( [simpleTextKeywords containsObject:listOfNegativeKeywords[i]])
matches = false;
}
}
return matches;
}
最佳答案
将肯定和否定关键字的列表以及要检查的文本转换为 NSSet
的实例。然后使用 NSSet
containsObject:
方法检查包含情况。
使用NSString
方法componentsSeparatedByString:
将要搜索的文本拆分为字符串数组。
使用 NSString
方法 stringByTrimmingCharactersInSet:
和使用 NSCharacterSet
方法创建的集合:characterSetWithCharactersInString:
进行修剪不需要的字符。
修剪示例代码:
NSString *trimCharacters = @"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?";
NSCharacterSet *trimCharacterSet = [NSCharacterSet characterSetWithCharactersInString:trimCharacters];
NSMutableArray *simpleTextKeywords = [NSMutableArray new];
for (NSString *item in textArray) {
[simpleTextKeywords addObject:[item stringByTrimmingCharactersInSet:trimCharacterSet]];
}
关于c# - 检查文本中的正负字符串(C# 到 Objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653379/