Cant replace string in NSMutableString - iphone

This is my string:
2011-10-07T08:55:16-05:00
I am trying to remove the colon with this code:
NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:#":"])
[dateString replaceCharactersInRange:range withString:#""];
My code enters the if statement, so I know that it found the colon. But it crashes with no errors on the last line. What am I doing wrong?

try this :
NSMutableString *dateString = [NSMutableString stringWithString:#"2011-10-07T08:55:16-05:00"];
NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:#":"])
[dateString replaceCharactersInRange:range withString:#""];

Related

Parse html NSString with REGEX [duplicate]

This question already has answers here:
Convert first number in an NSString into an Integer?
(6 answers)
Objective-C: Find numbers in string
(7 answers)
Closed 10 years ago.
I have NSString with couple strings like this that the 465544664646 is change between them :
data-context-item-title="465544664646"
How i parse the 465544664646 string to a Array ?
Edit
NSRegularExpression* myRegex = [[NSRegularExpression alloc] initWithPattern:#"(?i)(data-context-item-title=\")(.+?)(\")" options:0 error:nil];
[myRegex enumerateMatchesInString:responseString options:0 range:NSMakeRange(0, [responseString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [match rangeAtIndex:1];
NSString *string =[responseString substringWithRange:range];
NSLog(string);
}];
Try this one:
NSString *yourString=#"data-context-item-title=\"465544664646\" data-context-item-title=\"1212121212\"";
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:yourString];
[scanner scanUpToString:#"\"" intoString:nil];
while(![scanner isAtEnd]) {
NSString *tempString;
[scanner scanString:#"\"" intoString:nil];
if([scanner scanUpToString:#" " intoString:&tempString]) {
[substrings addObject:tempString];
}
[scanner scanUpToString:#"\"" intoString:nil];
}
//NSLog(#"->%#",substrings); //substrings contains all numbers as string.
for (NSString *str in substrings) {
NSLog(#"->%ld",[str integerValue]); //converted each number to integer value, if you want to store as NSNumber now you can store each of them in array
}
Something like this?
-(NSString *)stringFromOriginalString:(NSString *)origin betweenStartString: (NSString*)start andEndString:(NSString*)end {
NSRange startRange = [origin rangeOfString:start];
if (startRange.location != NSNotFound) {
NSRange targetRange;
targetRange.location = startRange.location + startRange.length;
targetRange.length = [origin length] - targetRange.location;
NSRange endRange = [origin rangeOfString:end options:0 range:targetRange];
if (endRange.location != NSNotFound) {
targetRange.length = endRange.location - targetRange.location;
return [origin substringWithRange:targetRange];
}
}
return nil;
}
You can use
- (NSArray *)componentsSeparatedByString:(NSString *)separator
method like
NSArray *components = [#"data-context-item-title="465544664646" componentsSeparatedByString:#"\""];
Now you should got the string at 2. index
[components objectAtIndex:1]
Now you can create array from that string using the method here
NSString to NSArray

I want to show the UILabel text "sunday" to uppercase "SUN" like that

NSString *strDay = [dic objectForKey:#"day"];
NSString *uppercaseString = [strDay uppercaseString];
cell.dayLabel.text = uppercaseString;
Is that the correct method to get that? But I get only uppercase. I want "sunday" to be shown in view like "SUN".
How about just this
NSString *uppercaseString = [strDay uppercaseString];
cell.dayLabel.text = [uppercaseString substringToIndex:3];
Assuming it has a valid day
NSString *strDay = [dic objectForKey:#"day"];
NSString* split = [strDay substringToIndex:3];
split=[split uppercaseString];
NSLOG (#"%#",split);
Cheers!
NSString *strDay = #"sunday";
NSString *newStr = [strDay substringToIndex:3];
NSString *uppercaseString = [newStr uppercaseString];
NSString *strDay = [dic objectForKey:#"day"];
cell.dayLabel.text = [strDay.uppercaseString substringToIndex:3]

How to get substring fom a string in iphone?

I've got some strings in the format:
="Netherlands Antillean Guilder (ANG)","Australian Dollar (AUD)"
I want to get the sub string between the parenthesis "()"
How do I do this?
Sloppy way to do it:
NSString *theString = #"Netherlands Antillean Guilder (ANG)";
NSArray *someArray = [theString componentsSeparatedByString:#"("];
theString = [someArray objectAtIndex:1];
someArray = [theString componentsSeparatedByString:#")"];
theString = [someArray objectAtIndex:0];
theString should be "ANG". Don't have SDK nearby, so there might be a mistake or two.
Hope it helps
NSString *startfrom = [[NSString alloc] initWithFormat:#"("];
NSString *Myword = nil;
NSScanner *scanner = [NSScanner scannerWithString:"Netherlands Antillean Guilder (ANG)","Australian Dollar (AUD)"];
[scanner scanUpToString:startfrom intoString:nil];
[scanner scanUpToString:#")" intoString:&Myword];
NSString *finalword = [Myword substringFromIndex:[startfrom length]];
NSLog(#" %#",finalword);
[startfrom release];

String with allowed chars

Is there a clean way to get a string containing only allowed characters?
for example:
NSString* myStr = #"a5&/Öñ33";
NSString* allowedChars = #"abcdefghijklmnopqrstuvwxyz0123456789";
NSString* result = [myStr stringWIthAllowedChrs:allowedChars];
result should now be #"a533";
It's not the cleanest, but you could separate the string using a character set, and then combine the resulting array using an empty string.
// Create a character set with every character not in allowedChars
NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:allowedChars] invertedSet];
// Split the original string at any occurrence of those characters
NSArray *splitString = [myStr componentsSeparatedByCharactersInSet:charSet];
// Combine the result into a string
NSString *result = [splitString componentsJoinedByString:#""];
Simple and easy to customize and understand approach:
NSString* myStr = #"a5&/Öñ33";
NSString* allowedChars = #"abcdefghijklmnopqrstuvwxyz0123456789";
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:allowedChars] invertedSet];
NSString *result = myStr;
NSRange range = [result rangeOfCharacterFromSet:set];
while (range.location != NSNotFound)
{
result = [result stringByReplacingCharactersInRange:range withString:#""];
range = [result rangeOfCharacterFromSet:set];
}
NSLog(#"%#", result);
One of the simplest-
NSString* result = #"";
for(NSUInteger i = 0; i < [myStr length]; i++)
{
unichar charArr[1] = {[myStr characterAtIndex:i]};
NSString* charString = [NSString stringWithCharacters:charArr length:1];
if([allowedChars rangeOfString:charString].location != NSNotFound)
result = [result stringByAppendingString:charString];
}
return result;

Extract an NSString using NSScanner

I'm fetching data from AllContacts; in that data I'm getting contact details such as (998) 989-8989. Using this number, I'm not able to make a call. Can any one help out with this? Thanks in advance.
HI All
At last i have used this following code to resolve this issue
NSString *originalString = #"(998) 989-8989";
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(#"%#", strippedString);
Thanks All
Sounds like you can just remove spaces, brackets and hypens.
NSString *phoneNumber = #"(998) 989-8989";
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumber = [#"tel://" stringByAppendingString:phoneNumber];
[[UIApplication sharedApplication] openURL:phoneNumber];