Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2009, 09:51 AM   #1 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Question Removing all punctuation and placing remaining words into array

I'm trying to take the text typed or pasted into a searchbar and remove all punctuation and separate each individual word and place it in an array. This is as close as I've gotten but there are a lot of empty entries in the array where the punctuation was.

Code:
NSString *sourceData = source.text;
NSString *filteredData = [[sourceData componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@"_"];
NSArray *components = [filteredData componentsSeparatedByString:@"_"];
Code:
   S,
    "",
    history,
    "",
    affecting,
    "",
    "",
    "",
    states,
    over,
    an,
    area,
    of,
    "",
    "",
There must be a better way of doing this. I need a very efficient way.

Last edited by AndrewSpeaksOut; 11-08-2009 at 10:11 AM.
AndrewSpeaksOut is offline   Reply With Quote
Old 11-08-2009, 10:29 AM   #2 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 679
Default

i can show you how to remove the punctiation easily.. i dont understand the rest.. are you trying to detect a word and then put a space???
anyways...
lets say you have a UITextView called mytextfield1 ... if it has a comma in it.. you can replace that comma with a space.... (.m file)
Code:
NSString *temptextFieldName = mytextfield1.text;
temptextFieldName = [temptextFieldName stringByReplacingOccurrencesOfString:@"," withString:@" "];
[mytextfield1 setText:temptextFieldName];
you could even replace a comma with a carriage return (going to the next line) like this...

Code:
NSString *temptextFieldName = mytextfield1.text;
temptextFieldName = [temptextFieldName stringByReplacingOccurrencesOfString:@"," withString:@" \n"];
[mytextfield1 setText:temptextFieldName];
the \n is just like hitting the return/enter key

GOOD LUCK!
orange gold is offline   Reply With Quote
Old 11-08-2009, 10:38 AM   #3 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Well, once I get rid of the punctuaion, I want to split the resulting string into individual components and place each component in an array.

Here's what I have now:

Code:
NSString *sourceData = source.text;
NSString *filteredData = [sourceData stringByReplacingOccurrencesOfString:@"," withString:@" "];
NSArray *components = [filteredData componentsSeparatedByString:@" "];
NSLog(@"%@", components);
But the way I'm doing it I get null entries in the array where the comma used to be.

Code:
2009-11-08 10:33:22.868 AppName[5619:207] (
    Andrew,
    "",
    Doug,
    "",
    Jim,
    ""
)
Maybe this is more of an array issue at this point I don't really know.
AndrewSpeaksOut is offline   Reply With Quote
Old 11-08-2009, 01:40 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Just do it without removing the comma first.

NSArray *components = [filteredData componentsSeparatedByString:@","];

Tom.
harrytheshark is offline   Reply With Quote
Old 11-08-2009, 01:53 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Or better yet, something like this:

Code:
NSString * string = @"Hello.World/Something!Else,Last Two";
NSArray * array = [string componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]];
NSLog(@"%@",array);
Tom.
harrytheshark is offline   Reply With Quote
Old 11-08-2009, 02:39 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 98
Default

Quote:
Originally Posted by AndrewSpeaksOut View Post
I'm trying to take the text typed or pasted into a searchbar and remove all punctuation and separate each individual word and place it in an array. This is as close as I've gotten but there are a lot of empty entries in the array where the punctuation was.

Code:
NSString *sourceData = source.text;
NSString *filteredData = [[sourceData componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@"_"];
NSArray *components = [filteredData componentsSeparatedByString:@"_"];
There must be a better way of doing this. I need a very efficient way.

You can do it this way which is much more efficient:

Code:
NSString *Source = @"Bla1;' Bla2#^ Bla3";
  NSCharacterSet *AcceptedCharacterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
  Source = [[Source componentsSeparatedByCharactersInSet:AcceptedCharacterSet] componentsJoinedByString:@" "];
  NSArray *Words = [Source componentsSeparatedByString:@" "];
  int WordCounter = 0;
  for (WordCounter = 0; WordCounter < [Words count]; WordCounter++){
    NSLog(@"%@", [Words objectAtIndex:WordCounter]);
  }
This outputs:

Bla1
Bla2
Bla3
__________________
UK Flights
Latest UK Flight information right in your iPhone/iPod Touch. All major UK Airports are available in this brilliant iPhone Application. Download from the iTunes Store.

GuitarMonster: Overdrive
A guitar plugged to an overdrive effect, right in your pocket. Download from the iTunes Store.
XCHG is offline   Reply With Quote
Old 11-08-2009, 04:21 PM   #7 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Thank you all for the suggestions! For the moment I'm using this method:

Quote:
Originally Posted by harrytheshark View Post
Or better yet, something like this:

Code:
NSString * string = @"Hello.World/Something!Else,Last Two";
NSArray * array = [string componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]];
NSLog(@"%@",array);
Tom.
I changed it a bit and it seems to be accepting input and it places it correctly (I think) in the array. But, when I try to get an object from the array to update a TableViewCell text, I get EXC_BAD_ACCESS.

I have a declared NSArray called components in my header file.

Here's some of the code from implementation:
Code:
-(void) examineSource {
NSString *string = source.text;
components = [string componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]];
NSLog(@"%@", components);
[table reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
	[source resignFirstResponder];
	[self examineSource];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [components count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
	}
	
	cell.textLabel.text = [components objectAtIndex:indexPath.row];
	
	return cell;
}
I get the error at this line:
Code:
cell.textLabel.text = [components objectAtIndex:indexPath.row];
I think it may be because the objects in the components array are not strings? I don't know how to change them or use them as strings if that is the case. Appreciate all the help you all have been so far! Thanks!
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Old 11-08-2009, 04:29 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

It may just be as simple as the array isn't retaining the objects.

components = [[NSArray alloc] init];
components = [string componentsSeperated...];

Then make sure to release the array when you're done with it.
harrytheshark is offline   Reply With Quote
Old 11-08-2009, 04:41 PM   #9 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Quote:
Originally Posted by harrytheshark View Post
It may just be as simple as the array isn't retaining the objects.

components = [[NSArray alloc] init];
components = [string componentsSeperated...];

Then make sure to release the array when you're done with it.
I added that first line and no difference. However, I added a [components retain]; right before the [table reloadData]; and that did the trick. Although I am still confused where or why it was getting released before the cells could update?
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Old 11-08-2009, 05:02 PM   #10 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Darn! I just realized that there are empty objects in the array using either of the two suggested methods above.




Can this be cleaned up after the array has been created or do I need to find a different way to create the array with objects?
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Old 11-08-2009, 08:40 PM   #11 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

I think I may have narrowed it down to only happen between words that have multiple spaces, symbols, or numbers. a single space or single punctuation mark seem to be handled properly.

Any ideas?
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Old 11-09-2009, 04:03 AM   #12 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 98
Default

Quote:
Originally Posted by AndrewSpeaksOut View Post
I think I may have narrowed it down to only happen between words that have multiple spaces, symbols, or numbers. a single space or single punctuation mark seem to be handled properly.

Any ideas?
Read my previous post and find out where I have used componentsJoinedByString
__________________
UK Flights
Latest UK Flight information right in your iPhone/iPod Touch. All major UK Airports are available in this brilliant iPhone Application. Download from the iTunes Store.

GuitarMonster: Overdrive
A guitar plugged to an overdrive effect, right in your pocket. Download from the iTunes Store.
XCHG is offline   Reply With Quote
Old 11-10-2009, 07:35 PM   #13 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Here's what I did to remove null items from an NSMutableArray:

Code:
for (int i=0; i<[components count]; i++) {
if ([components objectAtIndex:i] == [NSString stringWithFormat:@""]) {
NSLog(@"nil found");
} else {
[muta addObject:[components objectAtIndex:i]];
}
}
Thanks for the help guys!
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Old 11-11-2009, 03:19 AM   #14 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 98
Default

Quote:
Originally Posted by AndrewSpeaksOut View Post
Here's what I did to remove null items from an NSMutableArray:

Code:
for (int i=0; i<[components count]; i++) {
if ([components objectAtIndex:i] == [NSString stringWithFormat:@""]) {
NSLog(@"nil found");
} else {
[muta addObject:[components objectAtIndex:i]];
}
}
Thanks for the help guys!
Hi,

Remember that you can not use == to see if a string is empty. You also can not insert nil/null into an array as it will raise an exception and the program will crash.

So what you can do is something like this:

Code:
NSMutableArray *MyArray = [[NSMutableArray alloc] init];
[MyArray addObject:@"One"];
[MyArray addObject:@""];
[MyArray addObject:@"Three"];

int Counter = 0;
for (Counter = 0; Counter < [MyArray count]; Counter++){
  NSString *ThisString = (NSString*) [MyArray objectAtIndex:Counter];
  if ([ThisString length] == 0){
    [MyArray removeObjectAtIndex:Counter];
    Counter = -1;
    continue;
  }
}
[MyArray release];
MyArray = nil;
I haven't written the above code in XCode. I just wrote it here so it might have some syntax errors but I think you get the idea. Make sure you use [NSString length] instead of what you did. Best of luck.
__________________
UK Flights
Latest UK Flight information right in your iPhone/iPod Touch. All major UK Airports are available in this brilliant iPhone Application. Download from the iTunes Store.

GuitarMonster: Overdrive
A guitar plugged to an overdrive effect, right in your pocket. Download from the iTunes Store.
XCHG is offline   Reply With Quote
Old 11-11-2009, 11:03 AM   #15 (permalink)
Registered Member
 
AndrewSpeaksOut's Avatar
 
Join Date: Sep 2009
Location: Tennessee
Posts: 14
Default

Good stuff, thanks again! I will make some adjustments like you suggested. Appreciate the help in educating a newbie.
__________________
Find me on Twitter // Find me on Wakoopa
AndrewSpeaksOut is offline   Reply With Quote
Reply

Bookmarks

Tags
components, nsarray, nscharacterset, nsstring

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 273
20 members and 253 guests
ADY, Bertrand21, Dani77, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, jimbo, macquitzon216, MACralik, mer10, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84, Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0