java-string

Get Started: Java String

In This Article, You Will Learn about Java String.

Java String – Before moving ahead, let’s know a bit about Method Overloading in Java.

Table of Contents

String

A String is used to store the sequence of characters in double quotes.

Note: String is a data type too in Java.

Example: Create a variable of type String and assign a value.

				
					String name = "Alex"
				
			

String Methods

String Length

A String in Java is an object which contains a method that can perform operations on strings. One of the methods of the string is finding out the length of the string by length() method.

				
					String name = "Mississippi River";
System.out.println("The length of the name string is: " + name.length());

				
			

charAt() Method

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

Syntax

public char charAt(int index)

Example: Find the character at index number 8.

				
					String name = "Mississippi River";
char result = name.charAt(8);
System.out.println(result)
				
			

endsWith() Method

The endsWith() method checks whether a string ends with the specified character(s).

Syntax

public boolean endsWith(String chars)

Example: Check whether the given string ends with a specified character or not.

				
					String name = "Mississippi River";
System.out.println(name.endsWith("er"));   // true
System.out.println(name.endsWith("e"));   // false
System.out.println(name.endsWith("iver"));   // true

				
			

startsWith() Method

The startsWith() method checks whether a string starts with the specified character(s).

Syntax

public boolean startsWith(String chars)

Example: Check whether the given string starts with a specified character or not.

				
					String name = "Mississippi River";
System.out.println(name.startsWith("Mi"));   // true
System.out.println(name.startsWith("ssi"));   // false
System.out.println(name.startsWith("iver"));   // false
				
			

toLowerCase() Method

The toLowerCase() method converts a string to lower case letters.

Syntax

public String toLowerCase()

Example: Use toLowerCase() method to convert a string to lower case letters.

				
					String name = "Mississippi River";
System.out.println(name.toLowerCase());
				
			

toUpperCase() Method

The toUpperCase() method converts a string to upper case letters.

Syntax

public String toUpperCase()

Example: Use toUpperCase() method to convert a string to upper case letters.

				
					String name = "Mississippi River";
System.out.println(name.toUpperCase());
				
			

Later in lesson you will learn more about Java String Methods. 

Java String Concatenation

String Concatenation

String Concatenation – String Concatenation is the process of concatenating two or more strings by using the + operator.

Example: Use + operator to perform concatenation between strings.

				
					String name1 = "Mississippi";
String name2 = "River";
System.out.println("The concatenation of the strings are: " + name1 + " " + name2);
				
			

Java Numbers and Strings

Adding Numbers and Strings

Note: Java uses the + operator for both addition and concatenation.

The addition is used for Numbers. 

Concatenation is used for Strings.

Example: Use + operator to add two numbers.

				
					int x = 10;
int y = 20;
int z = x + y;
System.out.println("The addition of the x + y + z are: " z);

				
			

Example: Use + operator to perform operation on the string.

				
					String num1 = "01";
String num2 = "02";
System.out.println("The concatenation of the strings are: " + num1 + " " + num2);

				
			

Example: Use + operator to perform operation on both string and number.

				
					String num1 = "01";
int y = 20;
String z = x + y;
System.out.println("The output is: " + z);

				
			

Output: Incompatible types: String cannot be converted to int.

Java Special Characters

Strings – Special Characters

Since, the string is written inside the double quote; therefore Java returns an error.

String sentenceLine = “I love to do coding “and taking tutorial” from codingstreets.”; 

To avoid the error, Java uses the backslash escape character.

The backslash (\) escape character turns special characters into string characters:

Escape character

Result

Description

\’

Single quote

\”

Double quote

\\

\

Backslash

\n

New Line

 

\r

Carriage Return

 

\t

Tab

 

\b

Backspace

 

The sequence \’  inserts a single quote in a string:

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line1 = "I\'m okay.";
    System.out.println(line1);
  }
}
				
			

The sequence \”  inserts a double quote in a string:

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line2 = "I love to do coding \"and taking tutorial\" from codingstreets."; 
    System.out.println(line2);
  }
}
				
			

The sequence \\  inserts a single backslash in a string:

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line3 = "The character \\ is called backslash.";
    System.out.println(line3);
  }
}
				
			

It \n  inserts a new line.

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line1 = "I love to \n learn coding.";
    System.out.println(line1);
  }
}
				
			

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line1 = "I love to \r learn coding.";
    System.out.println(line1);
  }
}
				
			

It \t  inserts tab size.

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line1 = "I love to \t learn coding.";
    System.out.println(line1);
  }
}
				
			

It \b  inserts backspace.

Example:

				
					public class Main {
  public static void main(String[] args) {
    String line1 = "I love to lea\brn coding.";
    System.out.println(line1);
  }
}
				
			

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on