Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One approach for you, far from unique:</p> <p><code>NString</code> has methods to find substrings, represented as an <code>NSRange</code> of location &amp; offset, made up from characters in a given <code>NSCharacterSet</code>.</p> <p>The set of what should be in the string:</p> <pre><code>NSCharacterSet *ATCG = [NSCharacterSet characterSetWithCharactersInString:@"ATCG"]; </code></pre> <p>And the set of what shouldn't:</p> <pre><code>NSCharacterSet *invalidChars = [ATCG invertedSet]; </code></pre> <p>You can now search for any range of characters consisting of <code>invalidChars</code>:</p> <pre><code>NSString *target; // the string you wish to check NSRange searchRange = NSMakeRange(0, target.length); // search the whole string NSRange foundRange = [target rangeOfCharacterFromSet:invalidChars options:0 // look in docs for other possible values range:searchRange]; </code></pre> <p>If there are no invalid characters then <code>foundRange.location</code> will be equal to <code>NSNotFound</code>, otherwise you change examine the range of characters in <code>foundRange</code> and produce your specialised error messages.</p> <p>You repeat the process, updating <code>searchRange</code> based on <code>foundRange</code>, to find all the runs of invalid characters.</p> <p>You could accumulate the found invalid characters into a set (maybe <code>NSMutableSet</code>) and produce the error messages at the end.</p> <p>You can also use regular expressions, see <code>NSRegularExpressions</code>.</p> <p>Etc. HTH</p> <p><strong>Addendum</strong></p> <p>There is a really simple way to address this, but I did not give it as the letters you give suggest to me you may be dealing with very long strings and using provided methods as above may be a worthwhile win. However on second thoughts after your comment maybe I should include it:</p> <pre><code>NSString *target; // the string you wish to check NSUInteger length = target.length; // number of characters BOOL foundInvalidCharacter = NO; // set in the loop if there is an invalid char for(NSUInteger ix = 0; ix &lt; length; ix++) { unichar nextChar = [target characterAtIndex:ix]; // get the next character switch (nextChar) { case 'A': case 'C': case 'G': case 'T': // character is valid - skip break; default: // character is invalid // produce error message, the character 'nextChar' at index 'ix' is invalid // record you've found an error foundInvalidCharacter = YES; } } // test foundInvalidCharacter and proceed based on it </code></pre> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload