获取多次出现的字符串之间的字符串
Get string between strings with multiple occurrences
我找到了很多示例 how to find string between 2 strings,但其中 none 展示了如何处理该字符串的多次出现。我有这样的字符串
"Hi, I am <id>
User</id>
. I am 20 <id>
years old</id>
, and live in <id>
some country</id>
."
背后的想法是,我想在 UITextField
中超链接该字符串的每个出现,并从字符串中删除标签。我还有 2 种类型的标签,一种应该显示超链接,另一种应该弹出警报视图,其中包含对所点击的单词或短语的一些文本描述。
编辑:
找到了一个非常好的工作解决方案来扩展这个逻辑,通过文本中提供的标签之间的属性字符串更改文本内容。 Link here.
@Aleksandar 试试这个..它对我有用..
NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>.";
if([serverOutput containsString:@"</id>"])
{
NSArray *arrSeparate = [serverOutput componentsSeparatedByString:@"</id>"];
NSString *output = @"";
for(int i=0; i<arrSeparate.count; i++)
{
if([[arrSeparate objectAtIndex:i] containsString:@"<id>"])
{
NSArray *arrTest = [[arrSeparate objectAtIndex:i] componentsSeparatedByString:@"<id>"];
if(output.length < 1)
output = [arrTest objectAtIndex:1];
else
output = [NSString stringWithFormat:@"%@\n%@",output,[arrTest objectAtIndex:1]];
}
}
serverOutput = output;
}
NSLog(@"%@", serverOutput);
请查看此内容,我希望这能让您了解关键字存在的所有范围
NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>";
NSUInteger count = 0, length = [serverOutput length];
NSRange startRange = NSMakeRange(0, length);
NSRange endRange = NSMakeRange(0, length);
while(startRange.location != NSNotFound)
{
startRange = [serverOutput rangeOfString: @"<id>" options:0 range:startRange];
if(startRange.location != NSNotFound)
{
endRange = [serverOutput rangeOfString: @"</id>" options:0 range:endRange];
startRange = NSMakeRange(startRange.location + startRange.length, length - (startRange.location + startRange.length));
endRange = NSMakeRange(endRange.location + endRange.length, length - (endRange.location + endRange.length));
count++;
}
}
startRange 是标签开始的范围,endRange 是开始的范围
您可以更改范围、位置、创建属性字符串并在字符串范围可用时添加超链接。
我找到了很多示例 how to find string between 2 strings,但其中 none 展示了如何处理该字符串的多次出现。我有这样的字符串
"Hi, I am <id>
User</id>
. I am 20 <id>
years old</id>
, and live in <id>
some country</id>
."
背后的想法是,我想在 UITextField
中超链接该字符串的每个出现,并从字符串中删除标签。我还有 2 种类型的标签,一种应该显示超链接,另一种应该弹出警报视图,其中包含对所点击的单词或短语的一些文本描述。
编辑:
找到了一个非常好的工作解决方案来扩展这个逻辑,通过文本中提供的标签之间的属性字符串更改文本内容。 Link here.
@Aleksandar 试试这个..它对我有用..
NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>.";
if([serverOutput containsString:@"</id>"])
{
NSArray *arrSeparate = [serverOutput componentsSeparatedByString:@"</id>"];
NSString *output = @"";
for(int i=0; i<arrSeparate.count; i++)
{
if([[arrSeparate objectAtIndex:i] containsString:@"<id>"])
{
NSArray *arrTest = [[arrSeparate objectAtIndex:i] componentsSeparatedByString:@"<id>"];
if(output.length < 1)
output = [arrTest objectAtIndex:1];
else
output = [NSString stringWithFormat:@"%@\n%@",output,[arrTest objectAtIndex:1]];
}
}
serverOutput = output;
}
NSLog(@"%@", serverOutput);
请查看此内容,我希望这能让您了解关键字存在的所有范围
NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>";
NSUInteger count = 0, length = [serverOutput length];
NSRange startRange = NSMakeRange(0, length);
NSRange endRange = NSMakeRange(0, length);
while(startRange.location != NSNotFound)
{
startRange = [serverOutput rangeOfString: @"<id>" options:0 range:startRange];
if(startRange.location != NSNotFound)
{
endRange = [serverOutput rangeOfString: @"</id>" options:0 range:endRange];
startRange = NSMakeRange(startRange.location + startRange.length, length - (startRange.location + startRange.length));
endRange = NSMakeRange(endRange.location + endRange.length, length - (endRange.location + endRange.length));
count++;
}
}
startRange 是标签开始的范围,endRange 是开始的范围
您可以更改范围、位置、创建属性字符串并在字符串范围可用时添加超链接。