NSPredicate Character and/or Letter Substitutions - nsstring

I've seen some, somewhat, similar questions about filtering out special characters in a predicate - but nothing suitable for my needs. I'm not really sure if what I want to do is even possible but I'll put it to the community instead:
I'm working with strings of pronunciations for a dictionary that contain a lot of IPA characters for instance:
ȵ
ɕ
ŋ
Ẓ
What I would like to do is set up some substitutions for these characters, something like:
n=ȵ
x=ɕ
ng=ŋ
r=Ẓ
in a search predicate so that a search for n* would result with anything beginning in ȵ, etc, etc...
Even other IPA 'letters' like "v" would be best resembled by a "w" if there is a simple way to switch in SEARCH letters for other STRING letters that would be great...

It seems like this should be done outside the predicate. Create a dictionary where the keys are the strings to be replaced and the values are the replacements. Then, have a loop which iterates over the dictionary and performs each replacement on the string that you will be searching. The resulting string after replacement is the string that you use in the predicate.

got the answer from here:
stackoverflow.com/questions/21852502/nspredicate-ignore-numbers-in-string-pinyin
an other question to which I asked...
this is the code [incase this is helpful for anyone else later] I used for string "pro" in class Words
- (NSString *)searchableStringValueTwo {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ȵ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"n"];
return searchString;
}
- (NSString *)searchableStringValueThree {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ɕ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"x"];
return searchString;
}
- (NSString *)searchableStringValueFour {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"ŋ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"ng"];
return searchString;
}
- (NSString *)searchableStringValueFive {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"Ẓ"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"r"];
return searchString;
}
- (NSString *)searchableStringValueSix {
NSCharacterSet *invalidSet = [NSCharacterSet characterSetWithCharactersInString:#"v"];
NSString *searchString = [[pro componentsSeparatedByCharactersInSet:invalidSet] componentsJoinedByString:#"w"];
return searchString;
}

Related

How do you compare versions in InstallScript?

It looks like there is a builtin function, VerCompare, but it requires strings that have all four components, e.g. 1.2.3.4. I haven't tried to do string manipulation in InstallScript and was hoping someone already had the code to take a version string and add .0's as necessary.
Needs some error checking, but here's the general idea:
prototype NUMBER CompareVersions(STRING, STRING);
prototype STRING FormatVersion(STRING);
function NUMBER CompareVersions(leftVersion, rightVersion)
STRING formattedLeftVersion, formattedRightVersion;
begin
formattedLeftVersion = FormatVersion(leftVersion);
formattedRightVersion = FormatVersion(rightVersion);
return VerCompare(formattedLeftVersion, formattedRightVersion, VERSION);
end;
function STRING FormatVersion(version)
STRING formattedVersion;
LIST tokens;
NUMBER count;
begin
tokens = ListCreate(STRINGLIST);
StrGetTokens(tokens, version, ".");
count = ListCount(tokens);
ListSetIndex(tokens, LISTLAST);
while (count < 4)
ListAddString(tokens, "0", AFTER);
count = count + 1;
endwhile;
StrPutTokens(tokens, formattedVersion, ".", FALSE);
ListDestroy(tokens);
return formattedVersion;
end;

Check if NSString "000001XX"is a number with intValue

Now I am trying to add a front end check on my app to detect if user input only number in the textfield.
I use:
- (IBAction)checkID:(UITextField *)sender {
if ([sender.text isEqualToString:#""]) {
sender.text = #"This information is required";
sender.backgroundColor =[UIColor redColor];
}else if (![sender.text intValue]) {
sender.text = [sender.text stringByAppendingString:#" is not valid number"];
sender.backgroundColor =[UIColor redColor];
}
NSLog(#"send.text is %#, intValue is %d",sender.text,[sender.text intValue]);
}
But I found it text begins with number and ends with string, its intValue is still the number.
In my text, text is "00001aa", but the intValue is 1.
Is there any other way to filter out this "00001aa" text?
Thanks in advance.
Yes, you can use NSRegularExpression, or NSCharacterSet (works for positive numbers).
For regular expressions, use ^[-+]?[0-9]+$.
NSRegularExpression *numEx = [NSRegularExpression
regularExpressionWithPattern:#"^[-+]?[0-9]+$" options:0 error:nil
];
NSLog(#"%ld", [numEx numberOfMatchesInString:#"-200" options:0 range:NSMakeRange(0, 4)]);
NSLog(#"%ld", [numEx numberOfMatchesInString:#"001A" options:0 range:NSMakeRange(0, 4)]);
For character set, use [NSCharacterSet decimalDigitCharacterSet].
BOOL isNum = [[NSCharacterSet decimalDigitCharacterSet]
isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:#"001AA"]
];

NSTimeInterval from text input

I got a question for anyone with a spare moment and an answer...
I need to change an NSString value to input into the AVAudioPlayer start offset
eg. player.currentTime = 0; I want to change the '0' to a readable value or an NSString to a numeral value.....
NSString has a integerValue function. You can also create an NSString using it's format class function and pass it a format string like this: "%d" and then your player.currentTime.
int timeInput = [[textField text] integerValue];
label.text = [NSString stringWithFormat:#"%d", timeInput];

How can I add leading whitespace in front of the NSString?

I need the String always have length 5...
for example, if
I pass hello", I get "hello"
I pass "xml", I get " xml" (two spaces before "xml");
I pass "i", I get " i" (four spaces before "i").
What is the most efficient way to do it? Thank you.
I suppose this might help:
int maxLength = 5;
NSString *text = #"xml";
NSString *result = [text stringByPaddingToLength:(maxLength-[text length]) withString:#" " startingAtIndex:0];
NSMutableString *yourString;//your string
NSMutableString *string;
if([string length]<5){
NSInteger length = 5-[string length];
for(int i=0;i<length;i++){
[string appendFormat:#" "];
}
[yourString appendString:string];
}
//now you can use yourString .it is formatted.

Building an NSstring out of an NSMutableArray

My eyes hurt from hours of trying to figure this one - and i have looked for an answer for quite a while on-line (it will be embarrassing to tell how much...).
all i am trying to do is to enumerate using a for-in loop on anExpression which is a NSMutableArray that holds NSNumbers and NSStrings.
my NSLog print for the variable ans returns an empty string.
What am i doing wrong?
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
I think you mean
ans = [ans stringByAppendingString:(NSString *)obj];
not just
[ans stringByAppendingString:(NSString *)obj];
NSStrings are immutable -- you can't append to them. -stringByAppendingString: returns a new string (which you could then assign to ans).
Alternatively, you might use an NSMutableString and the -appendString: method.
Hey, sorry for the bad coding format, posting it again ...
NSString *ans = #"";
for (id obj in anExpression)
{
if ([obj isKindOfClass:[NSString class]])
[ans stringByAppendingString:(NSString *)obj];
if ([obj isKindOfClass:[NSNumber class]])
[ans stringByAppendingString:(NSString *)[obj stringValue]];
NSLog(#"String so far: %# ", ans);
}
[ans autorelease];
NSLog(#"final string is: %# ", ans);
return ans;
the method stringByAppendingString: returns a new string made by appending the given string to the receiver.
so you want ans = [ans stringByAppendingString:obj];

Resources