How to Declare and Initialize a String in Java
There are two main ways to create Java Strings. One is by creating a String literal (see top line in picture below). The other is to explicitly create a new Object in memory (See second line below). I will avoid going into detail about how these differ as it involves discussing aliasing and how Java saves memory. Note: Unlike some other programming languages, in Java Strings must be enclosed in double quotes. Single quotes are used for chars, a primitive data type.
Important Methods of a Java String
- String Concatenation
- To concatenate Strings (join to Strings), the easiest way to do it is just use the + operator as shown in the example below
-
String abcAsVariable ="abc";
String defAsVariable ="def";
String abcdef = "abc" + "def";
You could achieve the same concatenation by using the variables as shown in the next line.
String abcdef = defAsVariable + abcAsVariable ;
String Methods API
.length() method |
returns int |
- The length() methods tells you how many characters (including white space) are in a String.
- Examples
-
Example 1)
String myStr="abc"
System.out.println(myStr.length())
3
Example 2)
String myStr2="a b34 "
System.out.println(myStr2.length())
5
Remember, you need to count the space between a and b!
The index position of characters in Strings
Before we look at the String methods indexOf() and substring() we first should take a look at how Java thinks about the position of each character in a String. Consider the String "abcdef". "a" is in the first position; "b" in the second and so and so forth. The position of these letters is known as its index. Below is a picture of how each letter's position is indexed by Java. Note, that like Java arrays, Strings start with the number 0. |
|
String methods that rely upon the index position
.indexOf(String stringToFind) |
returns int |
- the indexOf() method returns the index position of stringToFind. If stringToFind is there, then this method returns -1.
- Examples
-
Example 1)
Java Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("a");
System.out.print(whereIsIt)
|
Output
0
|
Example 2)
Java Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("g");
System.out.print(whereIsIt)
|
Output
6 |
Example 3)
Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("Z");
System.out.print(whereIsIt)
|
Output
-1 |
You can also use .indexOf() to search for multicharacter sequeences. In other words the parameter of indexOf() can be more than character. See the examples below.
Example 4)
Java Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("ab");
System.out.print(whereIsIt)
|
Output
0
|
Example 5)
the Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("cd");
System.out.print(whereIsIt)
|
Output
1 |
Example 6)
Code
String myString="abcdefg";
int whereIsIt = myString.indexOf("ge");
System.out.print(whereIsIt)
|
Output
-1 |
.substring(int startingPosition,int end) |
returns String |
Sometimes a picture really is worth a thousand words. Examine the picture below that diagrams how the two parameters of the substring method work. The trick is that the second parameter is not inclusive. So you start and include the first paramater, startingPosition, and include everything up to but not including the second parameter, end.
. substring(int startingPosition) |
returns String |
The substring() method is an overloaded method. This version that takes only 1 parameter starts at the index position and goes to the end of the String.
toCharArray(); |
returns char[] |
This is a handy little method that takes a String and breaks each letter up to create a Array of chars. A char is a primitive data type that represents a single character and is denoted with single quotes.
String myStr = "abcdefg";
char[] asCharArr = myStr.toCharArray();
for(int i=0;i <asCharArr.length;i++)
System.out.println(asCharArr[i]);