如何将trim个字符从一个位置iOS移到另一个位置(Objective C)?
How to trim characters from a position to another position in iOS (Objective C)?
我的问题是,我想 trim 从我的字符串中删除一些字符。我的字符串包含 xml。所以我需要 trim 个字符从 xml 开始标签到它的结束标签。我该怎么做?
例如:我的字符串包含以下 xml 代码。
<CategoriesResponse xmlns="https://abc.defg.com/hijk">
<MainCategories>
<CatID xsi:type="xsd:int">178</CatID>
<CatID xsi:type="xsd:int">150</CatID>
<CatID xsi:type="xsd:int">77</CatID>
<CatID xsi:type="xsd:int">33</CatID>
<CatID xsi:type="xsd:int">179</CatID>
</MainCategories>
<SubCategories>
//some needed elements should not be trimmed.
</SubCategories>
我需要 trim 从开始标签 <MainCategories>
到结束标签 </MainCategories>
。
怎么做??所以在这里我的起始字符将是 <MainCategories
并且结束字符将是 /MainCategories>
试试这个
NSString *str = @"<CategoriesResponse xmlns=\"https://abc.defg.com/hijk\"><MainCategories><CatID xsi:type=\"xsd:int\">178</CatID><CatID xsi:type=\"xsd:int\">150</CatID><CatID xsi:type=\"xsd:int\">77</CatID><CatID xsi:type=\"xsd:int\">33</CatID><CatID xsi:type=\"xsd:int\">179</CatID></MainCategories><SubCategories></SubCategories>";
NSRange startRange = [str rangeOfString:@"<MainCategories>"];
NSRange endRange = [str rangeOfString:@"</MainCategories>"];
NSString *replacedString = [str stringByReplacingCharactersInRange:NSMakeRange(startRange.location, (endRange.location+endRange.length)-startRange.location) withString:@""];
NSLog(@"%@",replacedString);
希望对您有所帮助。
试试下面的代码:
NSString *strXml=@"<CategoriesResponse xmlns=\"https://abc.defg.com/hijk\"><MainCategories><CatID xsi:type=\"xsd:int\">178</CatID><CatID xsi:type=\"xsd:int\">150</CatID><CatID xsi:type=\"xsd:int\">77</CatID><CatID xsi:type=\"xsd:int\">33</CatID><CatID xsi:type=\"xsd:int\">179</CatID></MainCategories><SubCategories></SubCategories>";
strXml=[strXml stringByReplacingOccurrencesOfString:@"<MainCategories>" withString:@"<MainCategories"];
strXml=[strXml stringByReplacingOccurrencesOfString:@"</MainCategories>" withString:@"/MainCategories>"];
NSLog(@"%@",strXml);
希望对您有所帮助:)
我的问题是,我想 trim 从我的字符串中删除一些字符。我的字符串包含 xml。所以我需要 trim 个字符从 xml 开始标签到它的结束标签。我该怎么做?
例如:我的字符串包含以下 xml 代码。
<CategoriesResponse xmlns="https://abc.defg.com/hijk">
<MainCategories>
<CatID xsi:type="xsd:int">178</CatID>
<CatID xsi:type="xsd:int">150</CatID>
<CatID xsi:type="xsd:int">77</CatID>
<CatID xsi:type="xsd:int">33</CatID>
<CatID xsi:type="xsd:int">179</CatID>
</MainCategories>
<SubCategories>
//some needed elements should not be trimmed.
</SubCategories>
我需要 trim 从开始标签 <MainCategories>
到结束标签 </MainCategories>
。
怎么做??所以在这里我的起始字符将是 <MainCategories
并且结束字符将是 /MainCategories>
试试这个
NSString *str = @"<CategoriesResponse xmlns=\"https://abc.defg.com/hijk\"><MainCategories><CatID xsi:type=\"xsd:int\">178</CatID><CatID xsi:type=\"xsd:int\">150</CatID><CatID xsi:type=\"xsd:int\">77</CatID><CatID xsi:type=\"xsd:int\">33</CatID><CatID xsi:type=\"xsd:int\">179</CatID></MainCategories><SubCategories></SubCategories>";
NSRange startRange = [str rangeOfString:@"<MainCategories>"];
NSRange endRange = [str rangeOfString:@"</MainCategories>"];
NSString *replacedString = [str stringByReplacingCharactersInRange:NSMakeRange(startRange.location, (endRange.location+endRange.length)-startRange.location) withString:@""];
NSLog(@"%@",replacedString);
希望对您有所帮助。
试试下面的代码:
NSString *strXml=@"<CategoriesResponse xmlns=\"https://abc.defg.com/hijk\"><MainCategories><CatID xsi:type=\"xsd:int\">178</CatID><CatID xsi:type=\"xsd:int\">150</CatID><CatID xsi:type=\"xsd:int\">77</CatID><CatID xsi:type=\"xsd:int\">33</CatID><CatID xsi:type=\"xsd:int\">179</CatID></MainCategories><SubCategories></SubCategories>";
strXml=[strXml stringByReplacingOccurrencesOfString:@"<MainCategories>" withString:@"<MainCategories"];
strXml=[strXml stringByReplacingOccurrencesOfString:@"</MainCategories>" withString:@"/MainCategories>"];
NSLog(@"%@",strXml);
希望对您有所帮助:)