Friday, January 14, 2011

Objective C: Remove certain characters from NSString

Sometimes when you work in Xcode in the Cocoa Framework you want to filter a NSString so that only certain characters are left. That can be numbers or letters or special characters. Following code snippet can do that for you.

//the String with the original text
NSString *unfilteredString = @"The sum of 1 + 2 = 3.";
//initialize a string that will hold the result
NSMutableString *resultString = [NSMutableString stringWithCapacity:unfilteredString.length];
NSScanner *scanner = [NSScanner scannerWithString:unfilteredString];
//define the allowed characters, here only numbers from one to three, equal and plus
NSCharacterSet *allowedChars = [NSCharacterSet characterSetWithCharactersInString:@"123+="];
while ([scanner isAtEnd] == NO) {
 NSString *buffer;
 if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {
  [resultString appendString:buffer];     
 } else {
  [scanner setScanLocation:([scanner scanLocation] + 1)];
//Print out the result String, will be 1+2=3   
NSLog (@"Result: %@", resultString);

Wednesday, January 5, 2011

Turn Eclipse project into Java project

Sometimes you have a project in Eclipse that needs to be changed into a Java Project. Lets call that furthermore "Example".

The description is made with a Dynamic Web Project as the example Java Project. With some easy and obvious changes you could also use this tutorial for a Static Web Project etc.

1. Create a dynamic web project as helper

2 Files will be needed later in the project you want to change into a Java project. We create a dynamic web project to get those two files. That is the .project and the .classpath file.

You can delete this temporary dynamic web project when we are done with everything.

To create the dynamic web project open eclipse and go to the project explorer. Right-click and select New > New Project. In the "Select a wizard" under Web choose "Dynamic Web Project".

Then hit Next and type in the next view the Project name, e.g. "Temp", and hit Finish.

2. Change the .project file of the actual project "Example"

To change the .project file you have to first navigate to the file in a file browser. In Windows you can use the Explorer to go to the project folder, in Mac OS the Finder.

The example project has already a .project file, but the file is invisible. That's why you have to make sure that you can see invisible files in your browser. Here is a good description how to show invisible files in Finder on Mac OS.

In Windows Explorer you click Extras > Folder Options > View and then select something along the lines "display invisible Folders and Files".

When the file shows up for you, you have to open it in a text editor. Then also open the .project file in the "Temp" project folder and copy the content in the "Example" .project file. But make sure that you don't override the name tag. It should still read "Example".

The file look somewhat like that:


3. Change the .classpath file of the actual project "Example"

The "Example" project has no .classpath file yet. We can copy the .classpath file from the "Temp" project into the project folder.

The .classpath is invisible as well, but with the changes above it should be visible in the file browser as well.

4. Add Project Facets

That the changes from above take effect you first have to completely shut down eclipse. After the restart of eclipse the project should already be a Java project. That the project compile correctly you need to add some required facets.

Therefore right-click in eclipse Project Explorer on the project folder. Then open Properties and go to Project Facets. Here check Dynamic Web Module and Java.

5. Add required libraries (JAR files) to the project

This steps describes how to add the 3rd party libraries that are required in the project. Normally those jar files are stored in WebContent/WEB-INF/lib.

To add them to the classpath, open the project properties like described in step 4. Then select Java Build Path. Under Libraries select "Add JARs..." and navigate to WebContent/WEB-INF/lib and mark all .jar files you need. After hitting OK the libraries are added and you are good to go.


If there are still errors in your project it could be because your project needs libraries from the Apache Tomcat. If you go right-click on the project. And then select Run As > Run on Server and go through the whole process the needed Library will be added.