Note that there are some explanatory texts on larger screens.

plurals
  1. POMatching strings, consider some characters are the same
    primarykey
    data
    text
    <p>please help me with this problem.</p> <p>I want to check if the <code>targetString</code> match the <code>keyword</code> or not. Consider some character may different, but should still return true.</p> <p>Example:</p> <pre><code>targetString = @"@ß&lt;" keyword = @"abc", @"∂B(", @"@Aß&lt;" result: all must return true. </code></pre> <p>(Matched.<code>targetString</code> and all <code>keyword</code> are the same.)</p> <p>Consider me have an array, contains list of character set that can be the same:</p> <pre><code>NSArray *variants = [NSArray arrayWithObjects:@"aA@∂", @"bBß", @"c©C&lt;(", nil] </code></pre> <p>So that when matching, with this rule, it can match as the example above.</p> <p>Here is what i've done so far (using recursion): </p> <pre><code>- (BOOL) test:(NSString*)aString include:(NSString*) keyWord doTrim:(BOOL)doTrim { // break recursion. if([aString length] &lt; [keyWord length]) return false; // First, loop through each keyword's character for (NSUInteger i = 0; i &lt; [keyWord length]; i++) { // Get @"aA@∂", @"bBß", @"c©C&lt;(" or only the character itself. // like, if the keyword's character is A, return the string @"aA@∂". // If the character is not in the variants set, eg. P, return @"P" char c = [keyWord characterAtIndex:i]; NSString *rs = [self variantsWithChar:c]; // Check if rs (@"aA@∂" or @"P") contains aString[i] character if([rs rangeOfString:[NSString stringWithCharacters:[aString characterAtIndex:i] length:1]].location == NSNotFound) { // If not the same char, remove first char in targetString (aString), recursion to match again. return [self test:[aString substringFromIndex:1] include:keyWord doTrim:NO]; } } // If all match with keyword, return true. return true; } - (NSString *) variantsWithChar:(char) c { for (NSString *s in self.variants) { if ([s rangeOfString:[NSString stringWithFormat:@"%c",c]].location != NSNotFound) { return s; } } return [NSString stringWithFormat:@"%c", c]; } </code></pre> <p>The main problem is, <code>variantsWithChar:</code> doesn't return the correct string. I don't know which datatype and which function should I use here. Please help.</p> <p>For thou who know ruby, here's the example in ruby. It work super fine!</p> <pre><code>require 'test/unit/assertions' include Test::Unit::Assertions class String def matching?(keyword) length &gt;= keyword.length &amp;&amp; (keyword.chars.zip(chars).all? { |cs| variants(cs[0]).include?(cs[1]) } || slice(1, length - 1).matching?(keyword)) end private VARIANTS = ["aA@∂", "bBß", "c©C&lt;("] def variants(c) VARIANTS.find { |cs| cs.include?(c) } || c end end assert "abc".matching?("@ß&lt;") </code></pre> <p>PS: The fact is, it's containt a japanese character set that sounds the same (like <code>あア</code>, <code>いイ</code>... for thou who know japanese)</p> <p>PS 2: Please feel free to edit this Question, since my engrish is sooo bad. I may not tell all my thought.</p> <p>PS 3: And, maybe some may comment about the performance. Like, search about 10,000 target words, with nearly 100 variants, each variant have at most 4 more same characters.</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.
 

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