Im having a issue with removing special characters from the string .I used the following code.But dint work.Please suggest me better logic
- (NSString *)trimmedReciString:(NSString *)stringName
{
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:#"-/:;()$&#\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
for (int i = 0; i < [stringName length]; i++) {
unichar c = [stringName characterAtIndex:i];
if ([myCharSet characterIsMember:c]) {
NSLog(#"%#",[NSString stringWithFormat:#"%c",[stringName characterAtIndex:i]]);
stringName = [stringName stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%c",[stringName characterAtIndex:i]] withString:#""];
}
}
return stringName;
}
NSString *s = #"$$$hgh$g%k&fg$$tw/-tg";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"-/:;()$&#\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"String is: %#", s);
Try this...
NSString *unfilteredString = #"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
Try starting from the end of the string and work backwards instead of going from front to back, since you're likely to accidentally (and unintentionally) skip characters when the previous character gets deleted.
Related
I have a string NSString *Original=#"88) 12-sep-2012"; or Original=#"8) blablabla";
I want to print only the characters before the ")" so how to find the index of the character ")". or how could i do it?
Thanks in advance.
To print the characters before the first right paren, you can do this:
NSString *str = [[yourString componentsSeparatedByString:#")"] objectAtIndex:0];
NSLog(#"%#", str);
// If you need the character index:
NSUInteger index = str.length;
U can find index of the character ")" like this:
NSString *Original=#"88) 12-sep-2012";
NSRange range = [Original rangeOfString:#")"];
if(range.location != NSNotFound)
{
NSString *result = [Original substringWithRange:NSMakeRange(0, range.location)];
}
You can use the following code to see the characters before ")"
// this would split the string into values which would be stored in an array
NSArray *splitStringArray = [yourString componentsSeparatedByString:#")"];
// this would display the characters before the character ")"
NSLog(#"%#", [splitStringArray objectAtIndex:0]);
NSUInteger index = [Original rangeOfString:#")"];
NSString *result = [Original substringWithRange:NSMakeRange(0, index)];
try the below code to get the index of a particular character in a string:-
NSString *string = #"88) 12-sep-2012";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#")"];
NSRange range = [string rangeOfCharacterFromSet:charSet];
if (range.location == NSNotFound)
{
// ... oops
}
else {
NSLog(#"---%d", range.location);
// range.location is the index of character )
}
and to get the string before the ) character use this:-
NSString *str = [[string componentsSeparatedByString:#")"] objectAtIndex:0];
Another soluation:
NSString *Original=#"88) 12-sep-2012";
NSRange range = [Original rangeOfString:#")"];
NSString *result = Original;
if (range.location != NSNotFound)
{
result = [Original substringToIndex:range.location];
}
NSLog(#"Result: %#", result);
I have used this code for replacing "\n" with "", but it is not working.
How can I do it?
NSString *descString = [values objectForKey:#"description"];
descString = [descString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSLog(#"Description String ---->>> %#",descString);
catlog.description = descString;
[webview loadHTMLString:catlog.description baseURL:nil];
webview.opaque = NO;
try this
NSString *s = #"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"%#", s);
descString = [descString stringByReplacingOccurrencesOfString:#"\\n" withString:#""];
For more information refer to stringByReplacingOccurrencesOfString:withString: method
Hope this helps you.
UPDATE
Swift 3.0
descString.replacingOccurrences(of: "\\n", with: "");
and If I am not wring it will also work for Swift 4.0
I have a string that contains words with consonants and vowels. How can I extract only consonants from the string?
NSString *str = #"consonants.";
Result must be:
cnsnnts
You could make a character set with all the vowels (#"aeiouy")
+ (id)characterSetWithCharactersInString:(NSString *)aString
then use the
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
method.
EDIT: This will only remove vowels at the beginning and end of the string as pointed out in the other post, what you could do instead is use
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
then stick the components back together. You may also need to include capitalized versions of the vowels in the set, and if you want to also deal with accents (à á è è ê ì etc...) you'll probably have to include that also.
Unfortunately stringByTrimmingCharactersInSet wont work as it only trim leading and ending characters, but you could try using a regular expression and substitution like this:
[[NSRegularExpression
regularExpressionWithPattern:#"[^bcdefghjklmnpqrstvwx]"
options:NSRegularExpressionCaseInsensitive
error:NULL]
stringByReplacingMatchesInString:str
options:0
range:NSMakeRange(0, [str length])
withTemplate:#""]
You probably want to tune the regex and options for your needs.
Possible, for sure not-optimal, solution. I'm printing intermediate results for your learning. Take care of memory allocation (I didn't care). Hopefully someone will send you a better solution, but you can copy and paste this for the moment.
NSString *test = #"Try to get all consonants";
NSMutableString *found = [[NSMutableString alloc] init];
NSInteger loc = 0;
NSCharacterSet *consonants = [NSCharacterSet characterSetWithCharactersInString:#"bcdfghjklmnpqrstvwxyz"];
while(loc!=NSNotFound && loc<[test length]) {
NSRange r = [[test lowercaseString] rangeOfCharacterFromSet:consonants options:0 range:NSMakeRange(loc, [test length]-loc)];
if(r.location!=NSNotFound) {
NSString *temp = [test substringWithRange:r];
NSLog(#"Range: %# Temp: %#",NSStringFromRange(r), temp);
[found appendString:temp];
loc=r.location+r.length;
} else {
loc=NSNotFound;
}
}
NSLog(#"Found: %#",found);
Here is a NSString category that does the job:
- (NSString *)consonants
{
NSString *result = [NSString stringWithString:self];
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:#"aeiou"];
while(1)
{
NSRange range = [result rangeOfCharacterFromSet:characterSet options:NSCaseInsensitiveSearch];
if(range.location == NSNotFound)
break;
result = [result stringByReplacingCharactersInRange:range withString:#""];
}
return result;
}
I have NSString like
Aavkar Complex, Opposite Gurukul, Drive-in Road, Ahmedabad,
àªà«àªàª°àª¾àª¤, India
so I want to consider only english characters and without
english characters are from above string so please give me any idea.
Thanks in advance.
Here is a little dirty example:
NSString *test = #"Olé, señor!";
NSMutableString *asciiCharacters = [NSMutableString string];
for (NSInteger i = 32; i < 127; i++) {
[asciiCharacters appendFormat:#"%c", i];
}
NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:asciiCharacters] invertedSet];
test = [[test componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] componentsJoinedByString:#""];
NSLog(#"%#", test); // Prints #"Ol, seor!"
try this:-
NSString *emailRegEx = #"[A-Za-z]";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
//Valid email address
NSString *textString=#"gagdaksdhaksdhaskdhasldhasldalasàªà«àªàª°àª¾àª¤dhwheqweuqweuqwe";
NSString *textFinalString=#"";
for (int i=0; i<[textString length]; i++) {
NSString *text2string=[textString substringWithRange:NSMakeRange(i,1)];
NSLog(#"%#",text2string);
if ([emailTest evaluateWithObject:text2string] == YES)
{
NSLog(#"yesenglishCharacter");
textFinalString=[textFinalString stringByAppendingString:text2string];
}
else {
NSLog(#"noenglishCharacter");
}
}
NSLog(#"textFinalString%#",textFinalString);
Here is some sample code using NSRange's to do this for a textfield, although the code should be easily adaptable to use for an array of NSString's. Hope that Helps!
my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];