+4 votes
74 views
in Tips & Tricks by (242k points)
reopened
Java: An introduction to Strings

1 Answer

+5 votes
by (1.6m points)
 
Best answer

Creating strings
What you can do with strings
Modifying strings

Strings are a useful component of almost any programming language. In this introduction we show you how Strings can be used in Java..

image image

A string basically is an array of characters. Strings are used in almost any programming language. The Java programming language provides a string wrapper class which is useful for creating and manipulating strings.

Creating strings

The easiest way to create a string is to use the following syntax:

String hello = "Hello World!";

"Hello World!" is a string literal that is assigned to the variable quote . This line of code is equivalent to creating a character array and calling the string constructor with it:

char[] hello = { 'H', 'e', 'l', 'l', 'o', ' ', 'W' , 'o', 'r', 'l', 'd', '!'};

String helloString = new String(hello);

In fact, every time the compiler reads a string literal enclosed in quotes in your code, it will automatically create a String object for that character sequence.

String concatenated = "ABC" + "DEF"; //"ABCDEF"

That means that the statement above will result in three string objects being created . The two strings that are used to create the concatenated one will be discarded immediately though.

Please keep in mind: Although the String class is a wrapper for the underlying character array, it can not be accessed directly by any means. Strings are immutable . That means you cannot directly change a String object . hello and concatenated in this case are only references to string objects somewhere in the memory. Thus, you can change them like any other variable. But each time you modify the content of a String reference, a new String with the desired content is created . This can cause some trouble when you want to modify a string very often, which might be the case if you want to concatenate many strings together. In such cases, you may consider using a StringBuilder for increased performance

What you can do with strings

As you might already know, you can print a string to the console using the standard print methods like println .

String hello = "Hello World!";
System.out.println(hello)

Often times it is important to know how many characters the string contains - or in other words - to determine the length of the string . This can be achieved by the length method .

String hello = "Hello World!";
int length = hello.length(); //Assign 12 to length

Since our String variables are only references , they cannot be compared with the logical operators such as == or ! = . Instead, you may use predefined functions from the String class.

String s1 = "Hello";
String s2 = "HELLO";

s1.equals(s2); // false
s1.equalsIgnoreCase(s2); // true
s1.compareTo(s2); // > 0 because 'e' comes after 'E'


The function equals compares the two strings and returns a boolean value whether the strings are equal or not. equalsIgnoreCase can be used if the capitalization of the two strings does not matter. compareTo can not only be used to check, if two strings are the same but also to sort them lexicographically . The function will return 0 if both strings are equal, a negative integer if the first string is "smaller" and a positive integer if the string is "greater"..

Modifying strings

Strings have some predefined methods for modifying them. Keep in mind though that each time these functions are called, a new string is being created that has to be assigned to a variable. If you choose to create a char array from the string, you can perform any changes on it and put it in a new string object. This is the simplest method of doing a custom modification to a string.

String s1 = "Hello";
String s2;
s2 = s1.toLowerCase(); // "hello"
s2 = s1.String toUpperCase(); // "HELLO"
s2 = s1.String trim(); // "Hello" (removes trailing and leading whitespaces)
s2 = s1.String replace('e', 'a'); // "Hallo"
char[] charArray = s1.toCharArray(); // create a char array from s1


...