string

How do I convert a String to a double with Java?

Question: How do I convert a String to a double with Java?

Answer: Converting a String to a double requires that you first convert the String to a Double object, then convert the Double object to a double data type (Double is an object, while double is a primitive data type).

Here's the source code for an example Java program that performs this conversion:

How to convert a Java String to a float

Java String/float FAQ: How do I convert a Java String to a float?

Converting a String to a float requires that you first convert the String to a Float object, then convert the Float object to a float data type (Float is an object, while float is a primitive data type).

An example of a simple program that performs this conversion is shown below:

Java String to number conversions

Java String to number FAQ: How do I convert a String to a number in Java?

You convert a Java String to a number using the methods of the class whose type you want to convert to. For instance, if you want to try to convert a String to an int, you use the parseInt() method of the Java Integer class, like this: 

How do I convert a String to a long with Java?

Question: How do I convert a String to a long with Java?

Answer: The Long class includes a method named parseLong() that serves just this purpose. An example of a simple program that performs this conversion is shown below:

A Java String to int conversion example

Java String to int FAQ: How do I convert a String to an int data type in Java?

Answer: You convert a String to an int using the parseInt method of the Java Integer class. The parseInt method converts the String to an int, and throws a NumberFormatException when the String can't be converted to an int type.

Let's take a look at two short examples.

Java split String - Split a String into tokens with StringTokenizer

Update: I updated this tutorial in late 2012 after the note from Pablo Souza in the Comments section showing that the StringTokenizer class is now a "legacy class."

To split a string in Java into substrings, use the splitmethod of the String class. For instance, given the following string:

String speech = "Four score and seven years ago";

You can split the string into substrings using the following line of code:

A Java method that tests if a String is null or blank

Problem: You're working on a Java application, and you're repeatedly performing a test to determine whether a String (or many strings) are either blank or null.

Solution

Create a simple Java method to perform this test for you. The following Java method returns true if a String is blank or null, otherwise it returns false:

Ruby read file example - read a file as a string

Here's a Ruby "read file" example program that shows how to read a file as one string. You just have to pass a file name in to the get_file_as_string method, and this Ruby method will read in all the records from the file, and return the contents of the file as a string.

Java clipboard - how to place text on the clipboard

Here's a "Java clipboard" method I use when writing a Java/Swing program that needs to place plain text (a String) on the system clipboard:

AppleScript dialog lists - how to display a list in a dialog

AppleScript dialog list FAQ: How can I display a list in an AppleScript dialog?

I couldn't find an example showing how to display a list of strings in an AppleScript dialog, so I created the following simple example. First I create a list (in this case a short list of voices on Mac OS/X that can be used with the say command), and then I display the list in a dialog the user can use to select one of the items in the list.

Syndicate content