How to truncate NSString? - iphone

I looked at the string formatting documents but couldn't figure out exactly how to do this.
Lets say I have a sting like this
#"(01–05) Operations on the nervous system"
I want to create 2 strings from this like so:
#"01-05" and #"Operations on the nervous system"
How can I do this?
Here are the docs I looked at: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

Give this a shot. It might be off a bit, I havent checked for typos. But you can mess around with it now that you get the idea.
NSString * sourceString = #"(01–05) Operations on the nervous system";
NSString *string1 = [sourceString substringToIndex:6];
string1 = [string1 stringByReplacingOccurrencesOfString:#"(" withString:#""];
//string1 = 01-05
NSString *string2 =[sourceString substringFromIndex:7];
//string2 = Operations on the nervous system

If you just want the first substring contained by the characters "(" and ")" and anything after that I'd recommend doing something like this:
NSString *original = #"(01–05) Operations on the nervous system";
NSString *firstPart = [NSString string];
NSString *secondPart = [NSString string];
NSScanner *scanner = [NSScanner scannerWithString:original];
[scanner scanUpToString:#"(" intoString:NULL]; // find first "("
if (![scanner isAtEnd]) {
[scanner scanString:#"(" intoString:NULL]; // consume "("
[scanner scanUpToString:#")" intoString:&firstPart]; // store characters up to the next ")"
if (![scanner isAtEnd]) {
[scanner scanString:#")" intoString:NULL]; // consume ")"
// grab the rest of the string
secondPart = [[scanner string] substringFromIndex:[scanner scanLocation]];
}
}
Of course the secondPart string will still have spaces and whatnot at the front of it, to get rid of those you can do something along the lines of:
secondPart = [secondPart stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
The advantage of using NSScanner is that you don't have to hard-code the start and end of the firstPart substring.

NSString *theFirstStringSubString = [NSString substringFromIndex:1];
NSString *theFirstStringSecondSubstring = [theFirstStringSubString substringToIndex:6];
Now theFirstStringSecondSubstring is 01-05
same thing for the other but at different indexes. Please note that these are strings that are autoreleased. If you want to keep them, retain it.

Related

How do I remove the end of an NSMutableString?

I have the following NSMutableString:
#"1*2*3*4*5"
I want to find the first * and remove everything after it, so my string = #"1"; How do I do this?
NSMutableString *string = [NSMutableString stringWithString:#"1*2*3*4*5"];
NSRange range = [string rangeOfString:#"*"];
if (range.location != NSNotFound)
{
[string deleteCharactersInRange:NSMakeRange(range.location, [string length] - range.location)];
}
You could try to divide this string by a separator and get the first object
NSString *result = [[MyString componentsSeparatedByString:#"*"]objectAtIndex:0];
After calling componentsSeparatedByString:#"*" you'll get the array of strings, separated by *,and the first object is right what you need.
Here's yet another strategy, using the very flexible NSScanner.
NSString* beginning;
NSScanner* scanner = [NSScanner scannerWithString:#"1*2*3*4*5"];
[scanner scanUpToString:#"*" intoString:&beginning];
You could use -rangeOfString: to find the index of the first asterisk and use that with -substringToIndex: to extract a substring from the original input. Something like this perhaps...
NSMutableString *input = #"1*2*3*4*5";
// Finds the range of the first instance. See NSString docs for more options.
NSRange firstAsteriskRange = [input rangeOfString:#"*"];
NSString *trimmedString = [input substringToIndex:firstAsteriskRange.location + 1];

Objective-c filter link from a tag in a NSString

I get an html back from the server formatted as this
www.mysite.com
it is an NSString
How can i filter my NSString so i only keep "www.mysite.com" between the <a> tags?
Use NSRegularExpression:
NSString *string = #"www.mysite.com";
string = [string stringByReplacingOccurrencesOfString:#"<.+?>" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
For more information about regex in objective-c, and more advanced examples, see the documentation: https://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
You can try with NSSCanner,
NSString *mystring = #"<status>SUCCESS</status>";
NSString *neededString = nil;
NSScanner *scanner =[NSScanner scannerWithString:mystring];
[scanner scanUpToString:#">" intoString:&neededString];
[scanner scanString:neededString intoString:NULL];
[scanner scanString:#">" intoString:NULL];
[scanner scanUpToString:#"<" intoString:&neededString];
NSLog(#"%#",neededString)
If you're certain enough of the format, you can get the index of the first > character and then the first < character after that, and then take the substring that starts after the first index and ends after the second.
There are multiple ways of doing this, including
NSArray *a = [ string componentsSeparatedByCharactersInSet: ... ]
and
NSRange r = [ string rangeOfCharacterFromSet: ... ];

Find characters from the given string with numbers.

How do I get string using NSScanner from a string which contains string as well as numbers too?
i.e. 001234852ACDSB
The result should be 001234852 and ACDSB
I am able to get numbers from the string using NSScanner and characters by using stringByReplacingOccurrencesOfString but I want to know, is that possible to get string from with the use of NSScanner or any other built in methods?
I would like to know the Regex for the same.
If you can guarantee that the string always consists of numbers followed by letters, then you could do the following with NSScanner:
NSScanner *scanner = [NSScanner scannerWithString:#"001234852ACDSB"];
NSString *theNumbers = nil;
[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
intoString:&theNumbers];
NSString *theLetters = nil;
[scanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet]
intoString:&theLetters];
A regular expression capturing the same things would look like this:
([0-9]+)([a-zA-Z]+)
Finally after google for the same and go through some information from net, I reached to my destination. With this I'm posting the code, this may help many who are facing the same problem as I have.
NSString *str = #"001234852ACDSB";
NSScanner *scanner = [NSScanner scannerWithString:str];
// set it to skip non-numeric characters
[scanner setCharactersToBeSkipped:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
int i;
while ([scanner scanInt:&i])
{
NSLog(#"Found int: %d",i); //001234852
}
// reset the scanner to skip numeric characters
[scanner setScanLocation:0];
[scanner setCharactersToBeSkipped:[NSCharacterSet decimalDigitCharacterSet]];
NSString *resultString;
while ([scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&resultString])
{
NSLog(#"Found string: %#",resultString); //ACDSB
}
You don't have to use a scanner to do it.
NSString *mixedString = #"01223abcdsadf";
NSString *numbers = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet]] componentsJoinedByString:#""];
NSString *characters = [[mixedString componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnouprstuwvxyz"] invertedSet]] componentsJoinedByString:#""];
For other possible solution view this question Remove all but numbers from NSString

UTF-8 conversion

I am grabbing a JSON array and storing it in an NSArray. However it includes JSON encoded UTF-8 strings, for example pass\u00e9 represents passé. I need a way of converting all of these different types of strings into the actual character. I have an entire NSArray to convert. Or I can convert it when it is being displayed, which ever is easiest.
I found this chart http://tntluoma.com/sidebars/codes/
Is there a convenient method for this or a library I can download?
thanks,
BTW, there is no way I can find to change the server so I can only fix it on my end...
You can use an approach based on the NSScanner. The following code (not bug-proof) can gives you a way on how it can work:
NSString *source = [NSString stringWithString:#"Le pass\\u00e9 compos\\u00e9 a \\u00e9t\\u00e9 d\\u00e9compos\\u00e9."];
NSLog(#"source=%#", source);
NSMutableString *result = [[NSMutableString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:source];
[scanner setCharactersToBeSkipped:nil];
while (![scanner isAtEnd]) {
NSString *chunk;
// Scan up to the Unicode marker
[scanner scanUpToString:#"\\u" intoString:&chunk];
// Append the chunk read
[result appendString:chunk];
// Skip the Unicode marker
if ([scanner scanString:#"\\u" intoString:nil]) {
// Read the Unicode value (assume they are hexa and four)
unsigned int value;
NSRange range = NSMakeRange([scanner scanLocation], 4);
NSString *code = [source substringWithRange:range];
[[NSScanner scannerWithString:code] scanHexInt:&value];
unichar c = (unichar) value;
// Append the character
[result appendFormat:#"%C", c];
// Move the scanner past the Unicode value
[scanner scanString:code intoString:nil];
}
}
NSLog(#"result=%#", result);
If you use the JSON Framework, then all you do is get your JSON string and convert it to an NSArray like so:
NSString * aJSONString = ...;
NSArray * array = [aJSONString JSONValue];
The library is well-written, and will automatically handle UTF8 encoding, so you don't need to do anything beyond this. I've used this library several times in apps that are on the store. I highly recommend using this approach.

How to replace a character in NSString without inserting a space?

Let's assume I have the string
NSString* myString = #"Hello,";
How can I remove the comma without leaving a space? I have tried:
NSString* newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
and
NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
But both are leaving spaces.
I just ran the following as a test
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
And I get 2010-04-05 18:51:18.885 TestString[6823:a0f] Helloxx as output. There is no space left.
NSString *newString = [myString substringToIndex:5];
That will ensure that there are only 5 characters in the string, but beware that this will also throw an exception if there are not at least 5 characters in the string to begin with. How are you handling this string? Is it being displayed to the user? Is it being written to a file? The code that you posted does not reproduce the error, so perhaps you should post the code that you are having a problem with.
the other way u can use.. by stringByReplacingCharactersInRange
token = [token stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"*"];
"token"is your want to replace the NSString and "i" is you want to change NSString by "*"