Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

String.valueOf - How to Convert an Float to String

public static Float valueOf(float f)
Returns a Float instance representing the specified float value. If a new Float instance is not required, this method should generally be used in preference to the constructor Float(float), as this method is likely to yield significantly better space and time performance by caching frequently requested values. Parameters: f - a float value.Returns: a Float instance representing f.

1. In the MainActivity.java file, add a Toast which displays the value of String.valueOf(3.567f);

Toast.makeText(this, String.valueOf(3.567f), Toast.LENGTH_LONG).show(); 

2. Compile and run!
3. The display results will be (as a string): 3.567

Resources:
http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#valueOf(float)

String.valueOf - How to Convert an Integer to String

public static String valueOf (int value) was added in API level 1
Converts the specified integer to its string representation.
Parameters: value - the integer.
Returns: the integer converted to a string
(** I prefer to use String.valueOf over, Integer.toString() **)

1. In the MainActivity.java file, add a Toast which displays the value of String.valueOf(1);

Toast.makeText(getBaseContext(), String.valueOf(1), Toast.LENGTH_LONG).show(); 

2. Compile and run!
3. The display results will be (as a string): 1

Resources:
http://stackoverflow.com/questions/3335737/integer-tostringint-i-vs-string-valueofint-i

How to Add a New Line/Line Break to a String

/n

To add a new line break to a string you just need to include a /n. For example, to have the below line break up

Example #1"This needs to be on two lines."
Result: This need to be on two lines.

Example #2"This needs to be" + "/n" + "on two lines."
Result: This needs to be
on two lines.

Resources:
http://stackoverflow.com/questions/5913166/displaying-multiple-lines-of-text-and-variables-in-an-alertdialog-using-setmessa

How to Create a String Array

Note: The size of an array can't be modified. If you want a bigger array a new array needs to be instantiated

1. If you don't already have your Android project, create your project.

2. Open the MainActivity.java file and add the below line in your onCreate method.
This will define and initialize a single array named myArray.

String[] myArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

3. Compile and test!

4. Another way to create it.
String[] myArray = new String[] { "January", "February", "March" }; 

5. Other useful String Arrays.

// Days of the Month
String[] myArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};

//Days of the Week
String[] myArray = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Next Recommended Article: How to Create an ArrayAdapter

Resources: