如何在iOS中查找字符串并替换其子字符串?

如何在iOS中查找字符串并替换其子字符串?,ios,objective-c,nsstring,Ios,Objective C,Nsstring,我有一根像下面这样的线 # Blender v2.72 (sub 0) OBJ File: 'untitled.blend' # www.blender.org mtllib test03.mtl o Cube.001 v 3.851965 0.040851 6.046364 v 3.851965 0.087396 6.092909 v -3.851965 0.087396 6.092909 # Blender v2.72 (sub 0) OBJ File: 'untitled.blend'

我有一根像下面这样的线

# Blender v2.72 (sub 0) OBJ File: 'untitled.blend'
# www.blender.org
mtllib test03.mtl
o Cube.001
v 3.851965 0.040851 6.046364
v 3.851965 0.087396 6.092909
v -3.851965 0.087396 6.092909
# Blender v2.72 (sub 0) OBJ File: 'untitled.blend'
# www.blender.org
mtllib test04.mtl
o Cube.001
v 3.851965 0.040851 6.046364
v 3.851965 0.087396 6.092909
v -3.851965 0.087396 6.092909
我需要阅读
3行(mtllib test03.mtl)
并将
test03.mtl
替换为
test04.mtl
。那么最后一行应该如下所示

# Blender v2.72 (sub 0) OBJ File: 'untitled.blend'
# www.blender.org
mtllib test03.mtl
o Cube.001
v 3.851965 0.040851 6.046364
v 3.851965 0.087396 6.092909
v -3.851965 0.087396 6.092909
# Blender v2.72 (sub 0) OBJ File: 'untitled.blend'
# www.blender.org
mtllib test04.mtl
o Cube.001
v 3.851965 0.040851 6.046364
v 3.851965 0.087396 6.092909
v -3.851965 0.087396 6.092909
我试着用下面的代码

NSString* str= @"mtllib test03.mtl";

// Search from back to get the last space character
NSRange range= [str rangeOfString: @"mtllib " options:NSBackwardsSearch];

// Take the first substring: from 0 to the space character
NSString* finalStr = [str substringToIndex: range.location];
NSLog(@"%@", finalStr);
但无法从上述行加载行(
mtllib test03.mtl
)。 我怎样才能解决这个问题


提前谢谢

您可以使用正则表达式:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^mtllib (.*)$" options:NSRegularExpressionAnchorsMatchLines error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"mtllib test04.mtl"];
if (error) {
    NSLog(@"Error: %@", error);
}
NSLog(@"%@", modifiedString);
不带regexp的解决方案:

NSString *str = @"# Blender v2.72 (sub 0) OBJ File: 'untitled.blend'\n\
# www.blender.org\n\
mtllib test03.mtl\n\
o Cube.001\n\
v 3.851965 0.040851 6.046364\n\
v 3.851965 0.087396 6.092909\n\
v -3.851965 0.087396 6.092909";
NSString *finalStr = str;

// Find "mtllib" substring
NSRange range= [str rangeOfString: @"mtllib " options:NSBackwardsSearch];
// This is location of filename, now we need to find it's range
CGFloat fileNameLocation = range.location + range.length;
// Find first end of line after "mtllib" substring
NSRange newlineRange = [str rangeOfString:@"\n" options:0 range:NSMakeRange(fileNameLocation, str.length-fileNameLocation)];
if (newlineRange.location != NSNotFound) {
    NSRange filenameRange = NSMakeRange(fileNameLocation, newlineRange.location - fileNameLocation);
    finalStr = [str stringByReplacingCharactersInRange:filenameRange withString:@"test04.mtl"];
} else {
    // Assume, there is no more data in string, only filename
    NSRange filenameRange = NSMakeRange(fileNameLocation, str.length - fileNameLocation);
    finalStr = [str stringByReplacingCharactersInRange:filenameRange withString:@"test04.mtl"];
}
NSLog(@"%@", finalStr);

“加载行失败”是什么意思?我需要替换
mtllib
后面的单词“加载行失败”是什么意思?您是否尝试过
stringByReplacingOccurrencesOfString
方法?这意味着我无法将其加载为
mtllib test03.mtl
谢谢。。使用正则表达式更好:)