ios - 如何在保留分隔符的同时拆分 NSString
<p><p>我正在使用以下内容以“,”分隔字符串:</p>
<pre><code>NSString * receivedString = @"component1,component3,,component6";
NSArray * arrayOfSubstrings = ;
</code></pre>
<p>但是我想保留“,”作为单独的组件并<strong>获取</strong>以下数组:</p>
<pre><code> "component1"
","
"component2"
","
","
"component6"
</code></pre>
<p><strong>而不是</strong>:</p>
<pre><code> "component1"
"component2"
"component6"
</code></pre>
<p>基本上,“,”也是我要分析的元素。</p>
<p>知道如何在不<strong>重新发明轮子</strong>的情况下做到这一点吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>(未测试)</p>
<pre><code>+ (NSArray *)splitString:(NSString *)string
withSeparator:(NSString *)separator
{
NSArray *elements = ;
NSMutableArray *arr = ;
for (NSUInteger i = 0; i < ; i++) {
NSString *element = elements;
if ( > 0)
;
if (i != - 1)
;
}
return arr;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在保留分隔符的同时拆分 NSString,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31938765/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31938765/
</a>
</p>
页:
[1]