How to use Java strings
Java strings are one of the complex data types in Java. They are used to represent and modify a sequence of characters or digits. There are a number of methods available to help you modify strings. In this tutorial we provide an introduction to what you need to know about strings in Java.
What are Java strings and what are they used for?
The programming language Java has many different data types. The most important ones include Java primitives like integer and char, and complex data types like strings. A string is a sequence of characters or digits that exists as an object in the java.lang class. Strings are created with the String class and then can no longer be changed. Despite this fact, Java strings are still used more often than StringBuilder, which allows you to modify strings after they’ve been created. Below we’ll explain how to create a string and what methods you have at your disposal.
- 99.9% uptime and super-fast loading
- Advanced security features
- Domain and email included
What is the syntax for Java strings?
If you want to create a new string in Java, you can use this simple syntax:
String name = "string";
javaYou signal to the system that you want to create a Java string, give the string a name and then enter the characters of the string between the quotation marks. If you want to refer to that string later, use the name you gave to it. Here’s what the code looks like for creating a string:
public class Main {
public static void main(String[] args) {
String example = "Here’s your first Java string.";
System.out.println(example);
}
}
javaThe output looks as follows:
Here’s your first Java string.
javaWhat are the methods used with Java strings?
Since Java strings are objects, there are a variety of methods you can use with them. Below we’ll introduce the most important ones.
length()
The length()
method is used to output the total number of character values in a string. You’ll get the length of your string as output. Here’s the syntax:
stringName.length()
javaIt’s also very simple to apply. The code looks as follows:
public class Main {
public static void main(String[] args) {
String example = "Here’s your first Java string.";
int length = example.length();
System.out.println("The string is " + length + " characters long.");
}
}
javaAnd you’ll get the following output:
The string is 30 characters long.
javaindexOf()
You can use the indexOf()
method to locate one or more substrings within your larger string. Here’s the syntax for it:
stringName.indexOf()
javaTo use the method, simply enter the part of the string that you want to locate.
public class Main {
public static void main(String[] args) {
String example = "Here’s your first Java string.";
System.out.println(example.indexOf("your"));
}
}
javaAs output you’ll get the position at which that text first appears:
7
javaconcat()
With concat()
you can connect two separate strings to form a new string. Here is its syntax:
stringName.concat(string1)
javaHere’s how the method works in practice:
public class Main {
public static void main(String[] args) {
String example = "word";
String newString = example.concat("smith");
System.out.println(newString);
}
}
javaHere’s the output:
wordsmith
javaequals()
equals()
is one of many methods you can use to compare Java strings with each other. The method returns a Boolean value (true or false). Here is its syntax:
string1.equals(string2)
javaIn our example, we’ll create three strings and then compare the first one with the other two:
public class Main {
public static void main(String[] args) {
String string1 = "ABC";
String string2 = "ABC";
String string3 = "DEF";
System.out.println(string1.equals(string2));
System.out.println(string1.equals(string3));
}
}
javaSince the first and second strings are identical and the third is not, you’ll get the following output:
true
false
javatoUpperCase()
und toLowerCase()
The two methods toUpperCase()
and toLowerCase()
serve to convert the character values within a string to uppercase or lowercase. Here is the syntax for each of them:
stringName.toUpperCase()
stringName.toLowerCase()
javaHere’s how they work:
public class Main {
public static void main(String[] args) {
String example = "Here’s your first Java string.";
System.out.println(example.toUpperCase());
System.out.println(example.toLowerCase());
}
}
javaHere’s the output:
HERE’S YOUR FIRST JAVA STRING.
here's your first java string.
javasplit()
With the Java split() method you can divide a Java string into smaller substrings. Here is its syntax:
String[] split(String regex)
javaThis is what it looks like in practice:
public class Main {
public static void main(String[] args) {
String x = "Here’s your first Java string";
String[] output = x.split(" ");
for (String y : output) {
System.out.println(y);
}
}
}
javaThe output looks like this:
Here’s
your
first
Java
string.
java