检查文本中的正字符串和负字符串(c#到objective C)
本文关键字:字符串 objective 文本 检查 | 更新日期: 2023-09-27 18:03:22
我有一个文本字符串,我想检查它的几个条件。我在c#中有这个函数,我想把它移植到objective C(我是新手)如果检查函数包含(1)中的任何一个而不包含(2)
,则应该返回true。(1)包含以下任意项:
keyword1
或
keyword2 AND keyword3
或
keyword4
(2)不应该包含这些关键字
keyword4
或
keyword5
或
keyword6
我在c#中使用LINQ完成了这个函数:
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]];
}