Subtracting 2 strings - iphone

I have a string say for example #"012" and I have another string #"02". How can I extract the difference of the 2 strings in iPhone Objective-C. I just need to remove the existence of the characters in the 2nd string from the first string.The answer would be "1".

Even shorter:
NSString* s1 = #"012";
NSString* s2 = #"02";
NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:s2];
NSString * final = [[s1 componentsSeparatedByCharactersInSet:set] componentsJoinedByString:#""];
NSLog(#"Final: %#", final);
This preserves the order of the characters in the original string.

You could do something like this;
NSString *s1 = #"012";
NSString *s2 = #"02";
NSCharacterSet *charactersToRemove;
charactersToRemove = [NSCharacterSet characterSetWithCharactersInString:s2];
NSMutableString *result = [NSMutableString stringWithCapacity:[s1 length]];
for (NSUInteger i = 0; i < [s1 length]; i++) {
unichar c = [s1 characterAtIndex:i];
if (![charactersToRemove characterIsMember:c]) {
[result appendFormat:#"%C", c];
}
}
// if memory is an issue:
result = [[result copy] autorelease];
Disclaimer: I typed this into the browser, and haven't tested any of this.

You're trying to do a set operation, so use sets.
{
NSString* s1 = #"012";
NSString* s2 = #"02";
NSMutableSet* set1 = [NSMutableSet set];
NSMutableSet* set2 = [NSMutableSet set];
for(NSUInteger i = 0; i < [s1 length]; ++i)
{
[set1 addObject:[s1 substringWithRange:NSMakeRange(i, 1)]];
}
for(NSUInteger i = 0; i < [s2 length]; ++i)
{
[set2 addObject:[s2 substringWithRange:NSMakeRange(i, 1)]];
}
[set1 minusSet:set2];
NSLog(#"set1: %#", set1);
// To get a single NSString back from the set:
NSMutableString* result = [NSMutableString string];
for(NSString* piece in set1)
{
[result appendString:piece];
}
NSLog(#"result: %#", result);
}

I simply used "componentsSeparatedByString"
NSString *s1 = #"abc";
NSString *s2 = #"abcdef";
//s2 - s1
NSString * final = [[s2 componentsSeparatedByString:s1] componentsJoinedByString:#""];

Related

How to random sort a NSString

NSMutableString *str =#"abcdefg123";
I want random the every character to a new String like this #"f1ad2g3be2".
NSMutableString *str1 = [[NSMutableString alloc]initWithString:str];
NSMutableString *str2 = [[NSMutableString alloc] init];
while ([str1 length] > 0) {
int i = arc4random() % [str1 length];
NSRange range = NSMakeRange(i,1);
NSString *sub = [str1 substringWithRange:range];
[str2 appendString:sub];
[str1 replaceOccurrencesOfString:sub withString:#"" options:nil range:range];
}
[str1 release];
str2 is what u want
Quite simple. First you must break up the characters into an array to work with. Then you swap the letters X many times, I choose to do this so every character will be swapped
NSString *str =#"abcdefg123";
int length = str.length;
NSMutableArray *letters = [[NSMutableArray alloc] init];
for (int i = 0; i< length; i++) {
NSString *letter = [NSString stringWithFormat:#"%c", [str characterAtIndex:i]];
[letters addObject:letter];
}
for (int i = 0; i<length; i++) {
int value = arc4random() % (length-1);
NSLog(#"Value is : %i", value);
[letters exchangeObjectAtIndex:i withObjectAtIndex:value];
}
NSString *results = [letters componentsJoinedByString:#""];
NSLog(#"The string before : %#", str);
NSLog(#"This is the string now : %#", results);

Change the position of character in string

I have a two string like "SANFRANSICO" and "CHICAGO"
Now i want to make it like the string is in reverse but in a single string like "OOCGIASCNIAHRCFNAS"
I have done code but it gives me wrong result
check out my code
- (NSString *)changeString1:(NSString *)string string2:(NSString *)string2{
int first = [string length];
int second = [string2 length];
int total = first+second;
NSMutableString *str = [[NSMutableString alloc] init];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for (int i=0; i<total+1; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j+1]];
NSLog(#"222 %d %#",j+1, ichar );
[str appendString:ichar];
check = YES;
}
}
else {
int j = i/2;
if(check == YES){
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j]];
NSLog(#"1111 %d %#",j, ichar );
[str appendString:ichar];
}
}
}
return str;
}
I found the answer very soon after post this question sorry for posting this question
check out my answer
- (NSString *)changeString1:(NSString *)string string2:(NSMutableString *)string2{
int first = [string length];
int second = [string2 length];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for(int i =second; i<first; i ++){
[string2 appendString:#" "];
}
int total = first*2;
NSMutableString *str = [[NSMutableString alloc] init];
for (int i=0; i<total; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
else {
int j = i/2;
if(j < first){
NSString *ichar = [NSString stringWithFormat:#"%c", [string characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:#"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
}
NSLog(#"str %#", str);
return str;
}

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;

Problem Adding URL (as string) to array

This is the problem code:
NSURL *url = [NSURL URLWithString:#"http://photostiubhart.comoj.com/ReadGallery/stiubhart1readgallery.php"];
NSError* error;
NSString* sizeString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
double myDouble = [sizeString doubleValue];
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5));
//Create an array to hold the URLS
NSMutableArray *myURLS;
//Initialize the array with nil
myURLS = [[NSMutableArray alloc] init];
NSLog(#"Here1");
//Add all the URLs from the server to the array
for (int i = 0; i <= myInt; i++){
NSString *tempString = [[NSString alloc] initWithFormat : #"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
myPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < myInt; i++){
[myPhotos addObject:[myURLS objectAtIndex:i]];
}
It gives the error:
2011-06-21 22:20:47.167 CHARLIE[666:207] -[NSURL length]: unrecognized selector sent to instance 0x70418c0
This is what the code does (at least supposed to):
Generate an integer from the contents of the URL ("4").
Use the integer in the for loops to add objects to arrays.
Can anyone tell me whats wrong here?
Thanks a lot,
Jack
If you want to add URLs as strings then shouldn't this –
for (int i = 0; i <= myInt; i++){
NSString *tempString = [[NSString alloc] initWithFormat : #"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject: [NSURL URLWithString:tempString]];
[tempString release];
}
be –
for (int i = 0; i <= myInt; i++){
NSString *tempString = [NSString stringWithFormat:#"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
[myURLS addObject:tempString];
}
You're probably expecting them to be strings and later calling length method on them whereas they are stored as URLs in the array.
Additionally, myPhotos seems to be a property in which case you can substitute -
myPhotos = [[NSMutableArray alloc] init];
for(int i = 0; i < myInt; i++){
[myPhotos addObject:[myURLS objectAtIndex:i]];
}
with this -
self.myPhotos = myURLS;
[myURLS release]; // To balance out the earlier alloc-init

How can I join a string repeatedly?

I want to do something like:
string s1 set to null
s2: "abc"
repeat 10 times
s1=s1+s2
How can I do this in objective-c?
Is this suitable?:
NSMutableString *s1 = [[NSMutableString alloc] initWithString:#""];
NSString *s2 = #"abc";
for(NSInteger idx = 0; idx < 10; ++idx) {
[s1 appendString:s2];
}
...
[s1 release];
Although #thatsdisgusting gave a perfect answer, here's a shortcut:
NSMutableString *a = [NSMutableString stringWithCapacity:0];
NSString *pad = #"abc";
NSString *ret = [a stringByPaddingToLength:10*[pad length] withString:pad
startingAtIndex:0];
abusing stringByPaddingToLength.