Java Strings - datacouch-io/spark-java GitHub Wiki

Understanding Strings

In Java, a String is a sequence of characters enclosed in double quotes. These characters can include letters, numbers, symbols, and even whitespace. Let's explore some fundamental aspects of working with strings in Java.

String Length

A String in Java is an object with methods that allow you to perform various operations on strings. One such operation is finding the length of a string using the length() method:

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length()); // Outputs: The length of the txt string is: 26

String Transformation

Java provides methods to transform strings to uppercase and lowercase. For example, you can use toUpperCase() and toLowerCase() methods:

String txt = "Hello World";
System.out.println(txt.toUpperCase());   // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase());   // Outputs "hello world"

Searching for Text

The indexOf() method helps you find the index (position) of the first occurrence of a specified text in a string, including whitespace:

String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7

Note that Java uses zero-based indexing.

String Concatenation

You can use the + operator to concatenate strings and create a new string:

String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName); // Output: John Doe

Alternatively, you can use the concat() method for string concatenation:

String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName)); // Output: John Doe

Handling Special Characters

When dealing with special characters within strings, it's important to use escape characters to prevent errors. Here are some common escape characters in Java:

  • \' for single quotes.
  • \" for double quotes.
  • \\ for a backslash.
String txt = "We are the so-called \"Vikings\" from the north."; // We are the so-called "Vikings" from the north.
String txt = "It\'s alright."; // It's alright.
String txt = "The character \\ is called backslash."; // The character \ is called backslash.

Additionally, you can use escape characters for special characters like:

  • \n for a new line.
  • \r for a carriage return.
  • \t for a tab.
  • \b for backspace.
  • \f for a form feed.

Understanding these fundamental concepts of strings is crucial for effective Java programming.