Learn about Strings in Android Java – XSTutorials

Learn about Strings in Android Java

PLEASE NOTE: This tutorial has been written using Android Studio 3.5 and Java

Overview

Java’s String class represents character strings. All string literals in Java programs, such as "Hello world", are implemented as instances of this class.

Strings are constant. Their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

Initialize an Empty String

To create an empty String, assign an empty string literal to a variable:

String myEmptyString = "";  // empty string

You can check if a String is empty in this way:

if (emptyString.isEmpty()) {
    Log.i("log-", "This string is empty!");
}
// prints: "This string is empty!"

This is another way to check it out:

if (emptyString.matches("")) {
    Log.i("log-", "This string is empty!");
}

String Literals

You can declare predefined String variable in multiple lines by separating each string with "" and adding the (+) sign at the end of each line:

String myString = "" +
    "Lorem ipsum dolor sit" +
    "consectetur adipiscing: " +
    "elit, sed do eiusmod " + 
    "tempor incididunt";

If you need to add line-breaks, use n:

String myString = "Lorem ipsum dolor sitn" +
              "consectetur adipiscing:n" +
              "elit, sed do eiusmodn" +
              "tempor incididunt";

Log.i("log-", "myString: " + myString);
/* Prints: myString: Lorem ipsum dolor sit
    consectetur adipiscing:
    elit, sed do eiusmod
    tempor incididunt
*/

Edit a String

You can edit a String by assigning it to a variable and append another string to it:

String variableString = "Hello";
variableString += " and welcome!";
Log.i("log-", variableString);
// Prints: Hello and welcome!

Working with Characters

You can access the individual characters (char)values for a String by iterating over the string with a for loop:

char[] data = {'H', 'e', 'l', 'l', 'o'};
for (char character : data) {
    Log.i("log-", String.valueOf(character));
}
// Prints:
// H
// e
// l
// l
// o

String values can also be constructed by passing an array of characters as an argument to its initializer:

char[] data = {'H', 'e', 'l', 'l', 'o'};
String myString = new String(data);
Log.i("log-", myString);
// Prints: Hello

How to Concatenate Strings and Characters

String values can be added together (or concatenated) with the addition operator (+) or (+=) to create a new String value:

String string1 = "Hello";
String string2 = " there";
String welcome = string1 + string2;
Log.i("log-", welcome);
// Prints: "Hello there"

String welcome = string1 += string2;
Log.i("log-", welcome);
// Prints: "Hello there"

How to Count Characters in a String

You can get the number of characters of a String in this way:

String myString = "Hello world, how is it going?";
// Prints: "myString has 29 characters"

Comparing Strings

Here’s an easy way to compare 2 strings:

String string1 = "Hello world!";
String sameString = "Hello world!";
if (string1.matches(sameString) ){
    Log.i("log-", "These two strings are equal");
}
// Prints: "These two strings are equal"

String Prefix and Suffix

You can check the prefix and suffix of a String in this way:

String myString = "Lorem ipsum dolor sit amet";
if (myString.startsWith("Lorem") ){
    Log.i("log-", "myString has 'Lorem' prefix");
}
// Prints: "myString has 'Lorem' prefix"

if (myString.endsWith("amet") ){
    Log.i("log-", "myString has 'amet' suffix");
}
// Prints: "myString has 'amet' suffix"

For more info about Java Strings, check the official Documentation: https://developer.android.com/reference/java/lang/String

Conclusion

That’s all for this tutorial, you have learned a little bit about Strings in Android Java.

Hope you enjoyed this article, feel free to post comments about it. 

Buy me a coffee - XScoder - thanks for your support
Your support will be highly appreciated 😉