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.
Related
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);
How to convert string to array in objective C. i.e,I have a string,
NSString *str = #"Hi,How r u";
This should be converted into an array *NSMutableArray arr , where in
arr[0] = "Hi"
arr[1] = ","
arr[2] = "How"
arr[3] = "r"
arr[4] = "u"
Can anybody help with the idea to crack this thing.
NSString *str=#"Hi,How r u";
NSArray *arr = [str componentsSeparatedByString:#","];
NSString *strSecond = [arr objectAtIndex:1];
NSMutableArray *arrSecond = [strSecond componentsSeparatedByString:#" "];
NSString *strHow = [arr objectAtIndex:0];
NSString *strAre = [arr objectAtIndex:1];
NSString *strYou = [arr objectAtIndex:2];
[arr removeObjectAtIndex:1];
[arr addObject:#","];
[arr addObject:strHow];
[arr addObject:strAre];
[arr addObject:strYou];
arr is the desired array.
I guess this link will help you.
NSString *str = #"Hi,How r u";
NSArray *listItems = [str componentsSeparatedByString:#","];
You have to do,
NSString *str = #"Hi,How r u";
NSArray *arr = [str componentsSeparatedByString:#" "];
And, in order for this to work as you expect, there should be a white-space between "Hi," and "How". Your string should look like #"Hi, How r u".
try this
NSString *str2=#"Hi,How r u";
NSMutableArray *arary = [[NSMutableArray alloc] initWithArray:[str2 componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#", "]]];
NSLog(#"%#",arary);
if you want , as a object
NSString *str2=#"Hi,How r u";
str2 = [str2 stringByReplacingOccurrencesOfString:#"," withString:#" , "];
NSMutableArray *arary = [[NSMutableArray alloc] initWithArray:[str2 componentsSeparatedByString:#" "]];
NSLog(#"%#",arary);
You can use the NSString method componentsSeparatedByString. Take a look at the reference here.
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
I have a string in the following format
myString = "cat+dog+cow"
I need to store each string separated by + in to a array.
Eg:
myArray[0] = cat
myArray[1] = dog
myArray[2] = cow
Can anyone tell me the proper way to do this?
componentsSeparatedByString: splits the string and return the result in an array.
NSArray *myArray = [myString componentsSeparatedByString:#"+"];
[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow
Try this..
NSArray *arr = [myString componentsSeparatedByString:#"-"];
[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome
It is vert simple..
NSString * test = #"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:#"-"];
// Now stringArray will contain all splitted strings.. :)
Hope this helps...
I you don't want use array then iterate through each character...
NSMutableString * splittedString = nil;
for(int i=0;i<test.length;i++){
unichar character = [test characterAtIndex:0];
if (character=='-') {
if (splittedString!=nil) {
NSLog(#"String component %#",splittedString);
[splittedString release];
splittedString = nil;
}
} else {
if (splittedString==nil) {
splittedString = [[NSMutableString alloc] init];
}
[splittedString appendFormat:#"%C",character];
}
}
if (splittedString!=nil) {
NSLog(#"String last component %#",splittedString);
[splittedString release];
splittedString = nil;
}
Thats all...
NSArray *myWords = [myString componentsSeparatedByString:#"+"];
You can find this one very simple
NSString *str = #"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:#"+"];
NSLog(#"Array items %#",arr);
OUTPUT:
Array items
(
Cat,
dog,
Cow
)
Use the componentsSeparatedByString: method of NSString.
NSString string = #"hai-welcome";
NSArray myArray = [string componentsSeparatedByString:#"-"];
NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];
NSArray *strArray = [myString componentsSeparatedByString:#"-"];
firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome
This will be the solution if you are dealing with a string:
NSString *mySstring = #"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: #"-"]];
And each word will be stored in respective position from 0-n.
Try This. :)
If you are averse to using arrays, you can consider this –
NSString *accessMode, *message;
NSScanner *scanner = [NSScanner scannerWithString:#"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:#"-"];
[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:#""] intoString:&message];
NSArray *lines = [string componentsSeparatedByString:#"-"];
The first value will be stored in 0th index of lines and second value will be stored in 1th index of lines..
Why not array?
Simple code :
NSString *myString = [[NSString alloc] initWithString:#"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:#"+"];
NSLog(#"1. %# 2. %# 3. %#",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);
Try This:
NSString *str = #"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:#"+"];
NSLog(#"%#",array) ;
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:#""];