Index

Android apps - Strings

AndoidStudio on Strings

All this is in app/java/com.(the project name)/MainActivity (in Java) unless otherwise specified.

Define a string
Length of a string
Substring
Test strings
Turn 'a' into zero, 'b' into 1, 'c' into 2, etc.
Data coming in from screen - must be changed to a string
Number to formatted string - for output, etc.
Concatenation of strings
String constants - in app/res/values/strings.xml
String constants array - in app/res/values/?.xml

Define a string

All variables in Java must be defined. Define global variables at the start of the class, before the "onCreate" method. Casual variables only used within a method can be defined within the method. Here is the definition for a variable called "stringFld". Please note that "String" starts with a capital while "double" and "int" don't. Sigh...
String stringFld = "";

Length of a string

int lenthString = joString.length()

where joString is a string (already defined).


Substring

string midString = joString.substring(i,j)

gives substring of the string joString, where i is the first character (first is zero) and j is the character AFTER the last character of the substring. So 'joString.substring(0,1)' would give just the first character of the string.


Test strings

Do not use '='!

if (str1.equals(str2)) {...}

Turn 'a' into zero, 'b' into 1, 'c' into 2, etc.

String str="Elephant"; char c = str.charAt(3); // this will put the Unicode value of the letter 'p' into c int n = c - 'a'; // this will subtract the Unicode for 'a' from this

This means that you can convert a letter into a numeric value (perhaps for using as a subscript for an array). Everything starts from zero, remember! So "str.charAt(3)" gives the fourth letter of "Elephant", which is "p". "n" ends up with 15 (as "p" is the 16th letter in the alphabet).


Data coming in from screen

Data coming into from an EditText needs to be converted into a string:
EditText oz = (EditText) findViewById(R.id.ozVal); String ozInput = oz.getText().toString();

It may then need to be converted into a number - see Numbers.


Number to formatted string (for output)

A number can be converted to a formatted string (which might be needed for output):

String gmOut = String.format("%4.2f", fld);

This gives two fixed decimal places and up to four numbers before the decimal point.

String number = String.format("%01d", fld);

This gives no decimal places, one digit.


Concatenation of strings

String string1Fld = string2Fld + string3Fld;
Sneaky way to make an integer into a string:
String stringFld = "" + intFld;

String constants

All string constants in apps should be defined in app/res/values/strings.xml like this:
<string name="ozName">"oz"</string> <string name="gmName">"gm"</string>
This can be used in app/res/layout/activity_main.xml like this (see Formatting the screen):
android:text="@string/ozName"
This can be used in app/java/com.(the project name)/MainActivity like this:
String stringFld = getString(R.string.ozName);

String constants array

You can define an array of strings with values. This is not done in app/res/values/strings.xml but in a new file. Right click on app/res/values/, select New, then "Values resource file" (top). Give it any file name you want, but it must end in ".xml". To quote the AndroidStudio manual, "The filename is arbitrary. The element's name will be used as the resource ID."
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array> </resources>

You use this (in app/java/com.(the project name)/MainActivity, in Java) like this:

Resources res = getResources(); String[] planets = res.getStringArray(R.array.planets_array);

© Jo Edkins 2016 - Index to Android app index