![](/img/trans.png)
[英]Objective C iOS UISegmentedControl changing position unexpectedly
[英]How to trim characters from a position to another position in iOS (Objective C)?
我的問題是,我想從字符串中刪除一些字符。 我的字符串包含xml。 因此,我需要從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>
我需要從開始標簽<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);
希望它會有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.