Is it possible to get an array to show up as text? - iphone

I'm trying to do something like this ..
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
[uitextviewOutlet setText:[NSArray arrayWithArray:array]];
and I'd like for that to show up on my uitextviewOutlet window, which is an object of UITextView that will print out text.
The code works if I straight out send the uitextviewOutlet object the setText message and if it takes string as the parameter, but it won't take the array.
is there a way to have it take an array?
TIA.

You can join the elements with, let's say a comma like this: NSString *joinedString = [array1 componentsJoinedByString:#","];
Edit_: I'm not a friend of "Do it for me", but here you go:
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
[uitextviewOutlet setText:[array componentsJoinedByString:#","]];
By the way, your code makes no sense, or do you fill up the array with more than just one value?

You can convert an array to a string with -componentsJoinedByString: as in #BjörnKaiser`s example. Or for more flexibility you can do:
NSString *string = [NSString stringWithFormat:#"Hello World"];
NSArray *array = [NSArray arrayWithObject:string];
for (NSString *araryItem in array) {
[uitextviewOutlet replaceRange:NSMakeRange(uitextviewOutlet.text.length, 0) withText:#"foo\n"];
[uitextviewOutlet replaceRange:NSMakeRange(uitextviewOutlet.text.length, 0) withText:arrayItem];
}

Related

Retrieve the substring of a strings which are in array

I am new to iphone.I have an array which contains the objects like below
"04_Num/04Num001.mp3",
"04_Num/04Num002.mp3",
"04_Num/04Num003.mp3",
"04_Num/04Num004.mp3",
"04_Num/04Num005.mp3",
"04_Num/04Num006.mp3",
"04_Num/04Num007.mp3",
"04_Num/04Num008.mp3",
"04_Num/04Num009.mp3",
"04_Num/04Num010.mp3",
"04_Num/04Num011.mp3",
"04_Num/04Num012.mp3",
"04_Num/04Num013.mp3",
"04_Num/04Num014.mp3",
"04_Num/04Num015.mp3",
"04_Num/04Num016.mp3",
"04_Num/04Num017.mp3",
"04_Num/04Num018.mp3",
"04_Num/04Num019.mp3",
"04_Num/04Num020.mp3",
"04_Num/04Num021.mp3",
"04_Num/04Num022.mp3",
"04_Num/04Num023.mp3",
"04_Num/04Num024.mp3",
"04_Num/04Num025.mp3",
"04_Num/04Num026.mp3",
"04_Num/04Num027.mp3",
"04_Num/04Num028.mp3",
"04_Num/04Num029.mp3",
"04_Num/04Num030.mp3",
"04_Num/04Num031.mp3",
"04_Num/04Num032.mp3",
"04_Num/04Num033.mp3",
"04_Num/04Num034.mp3",
"04_Num/04Num035.mp3",
"04_Num/04Num036.mp3"
but here i want retrieve the strings(objects)only after the / (i.e) for example 04_Num/04Num033.mp3 in this i want only the string 04Num033.mp3.Like this for all the above and then i have to place in an array
how it is possible if any body know this please help me...
lastPathComponent is what you need. You could do it like so:
NSMutableArray *files = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *file in songs) //Where songs is the array with the paths you have provided
{
[files addObject:[file lastPathComponent]];
}
You can separate the string into two parts using NSString's
componentsSeparatedByString:
method, and use the last string component
// Let's call your array of strings as stringsArray
NSMutableArray *prefixStrings = [[NSMutableArray alloc] init];
for (NSString *str in stringsArray) {
NSArray *stringComponents = [str componentsSeparatedByString:#"/"];
if ([stringComponents count]) {
[prefixStrings addObject:[stringComponents objectAtIndex:1]];
} }

NSRegularExpression to extract text

All,
I have a dictionary with two keys and values. I need to extract bits and pieces from them and place them into seperate strings.
{
IP = "192.168.17.1";
desc = "VUWI-VUWI-ABC_Dry_Cleaning-R12-01";
}
That is what the dictionary looks like when I call description.
I want the new output to be like this:
NSString *IP = #"192.168.17.1";
NSString *desc = #"ABC Dry Cleaning"; //note: I need to get rid of the underscores
NSString *type = #"R";
NSString *num = #"12";
NSString *ident = #"01";
How would I achieve this?
I've read through the Apple developer docs on NSRegularExpression but I find it hard to understand. I'm sure once I get some help once here I can figure it out in the future, I just need to get started.
Thanks in advance.
Okay, so first, you have to get the object associated with each key:
NSString *ip = [dic objectForKey:#"IP"]; //Btw, you shouldn't start a variable's name with a capital letter.
NSString *tempDesc = [dic objectForKey:#"desc"];
Then, what I would do is split the string in tempDesc, based on the character -.
NSArray *tmpArray = [tempDesc componentsSeparatedByString:#"-"];
Then you just have to get the strings or substrings you're interested in, and reformat them as needed:
NSString *desc = [[tmpArray objectAtIndex:2] stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSString *type = [[tmpArray objectAtIndex:3] substringToIndex:1];
NSString *num = [[tmpArray objectAtIndex:3] substringFromIndex:1];
NSString *ident = [tmpArray objectAtIndex:4];
As you can see, this works perfectly without using NSRegularExpression.

parsing array elements in iPhone

NSBundle *bundle = [NSBundle mainBundle];
NSString *pthpath = [bundle pathForResource:#"path" ofType:#"txt"];
NSString *content = [NSString stringWithContentsOfFile:pthpath encoding:NSUTF8StringEncoding error:nil];
array=[[NSArray alloc ]init];
array = [content componentsSeparatedByString:#"~"];
=====================================================================
here content is:
87,348~51,347~135,132~182,133~268,346~236,347~159,168~87,347#118,298~115,297~200,298~189,266~128,265~117,299#222,352~268,353~264,340~219,342~225,355#186,262~199,299~212,297~195,257~188,260
and array is:
"87,348",
"51,347",
"135,132",
"182,133",
"268,346",
"236,347",
"159,168",
"87,347#118,298",
"115,297",
"200,298",
"189,266",
"128,265",
"117,299#222,352",
"268,353",
"264,340",
"219,342",
"225,355#186,262",
"199,299",
"212,297",
"195,257",
"188,260"
But I want to again create an array by parsing with #. Please help me out...........
for (NSString *string in array) {
NSArray *subArray = [string componentsSeparatedByString:#"#"];
for (NSString *substring in subArray)
etc. etc.
(Next time try to have your question better formatted and articulated please.)
Instead of using componentsSeparatedByString:, use componentsSeparatedByCharactersInSet: and create a character set with the separators you want.
Also, you are creating an array there (array = [[NSArray alloc] init]) and when you do array = [content componentsSeparatedByString:#"#"] you are leaking the just allocated array. In general, seems like you should read more about how objects and references work.
I think from following code you may get some idea, if I understood your question correctly,
NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *tempArray1 = nil;
NSArray *tempArray2 = nil;
NSString *content = #"87,348~51,347~135,132~182,133~268,346~236,347~159,168~87,347#118,298~115,297~200,298~189,266~128,265~117,299#222,352~268,353~264,340~219,342~225,355#186,262~199,299~212,297~195,257~188,260";
tempArray1 = [content componentsSeparatedByString:#"#"];
for(NSString *string in tempArray1)
{
tempArray2 = [string componentsSeparatedByString:#"~"];
[resultArray addObjectsFromArray:tempArray2];
}
NSLog(#"ResultArray :%#", resultArray);

assign value from array to string

i have an array of 5 objects.
i want to assign object which is at index 1, to an NSSTRING.
nsstring *abc = [array objectAtindex:1];
i know this is wrong syntax, this is returning object , something like this.
how can i get value which is at index 1 and assign it to an string?
regards
Erm.. this is the correct syntax :)
Apart the name of the string class:
NSString *abc = [array objectAtIndex:1];
mind that this won't create a copy of the string, if you need to copy it use
NSString *abc = [NSString stringWithString:[array objectAtIndex:1]];
As Eiko notes you can directly copy the string object if you need to:
NSString *abc = [[array objectAtIndex:1] copy];
Arrays are zero based in Objective-C land. So...
NSArray *array = [NSArray arrayWithObjects:#"one", #"two", nil];
NSString *abc = [array objectAtIndex:1];
Would return the second object in the array. Zero would return the first.

Split one string into different strings

i have the text in a string as shown below
011597464952,01521545545,454545474,454545444|Hello this is were the message is.
Basically i would like each of the numbers in different strings to the message eg
NSString *Number1 = 011597464952
NSString *Number2 = 01521545545
etc
etc
NSString *Message = Hello this is were the message is.
i would like to have that split out from one string that contains it all
I would use -[NSString componentsSeparatedByString]:
NSString *str = #"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
NSArray *firstSplit = [str componentsSeparatedByString:#"|"];
NSAssert(firstSplit.count == 2, #"Oops! Parsed string had more than one |, no message or no numbers.");
NSString *msg = [firstSplit lastObject];
NSArray *numbers = [[firstSplit objectAtIndex:0] componentsSepratedByString:#","];
// print out the numbers (as strings)
for(NSString *currentNumberString in numbers) {
NSLog(#"Number: %#", currentNumberString);
}
Look at NSString componentsSeparatedByString or one of the similar APIs.
If this is a known fixed set of results, you can then take the resulting array and use it something like:
NSString *number1 = [array objectAtIndex:0];
NSString *number2 = [array objectAtIndex:1];
...
If it is variable, look at the NSArray APIs and the objectEnumerator option.
NSMutableArray *strings = [[#"011597464952,01521545545,454545474,454545444|Hello this is were the message is." componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#",|"]] mutableCopy];
NString *message = [[strings lastObject] copy];
[strings removeLastObject];
// strings now contains just the number strings
// do what you need to do strings and message
....
[strings release];
[message release];
does objective-c have strtok()?
The strtok function splits a string into substrings based on a set of delimiters.
Each subsequent call gives the next substring.
substr = strtok(original, ",|");
while (substr!=NULL)
{
output[i++]=substr;
substr=strtok(NULL, ",|")
}
Here's a handy function I use:
///Return an ARRAY containing the exploded chunk of strings
///#author: khayrattee
///#uri: http://7php.com
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
return [stringToBeExploded componentsSeparatedByString: delimiter];
}