Java Program to check the beginning of a string


A string is a class in Java that stores a series of characters enclosed within double quotes. Those characters are actually String-type objects. The string class is available in the 'java.lang' package. Suppose we have given a string and our task is to find the beginning of that string. Let's assume the beginning as the first two or three characters of that string. To check the beginning of a string, we can use several built-in methods available in Java including substring() and charAt().

Java Program to Check the Beginning of a String

To check the beginning of a string we can use the following ways −

  • for loop

  • while loop

  • charAt() method

  • substring() metod

Before discussing these ways, let's understand the problem with the help of an example.

Instance

Input

String = "Tutorials Point";

Output

The beginning of given String is: Tu

Example 1

In this example, we will initialize a string and then, convert the string to a character array so that we can access the characters of the string. At last, we will use the for loop to print the first two characters of the array which is the beginning of given string.

public class Example1 {
   public static void main(String []args) {
      // initializing the string 
      String str = "Tutorials Point";
      // converting the string into character array
      char strChars[] = str.toCharArray();
      System.out.println("The given String is: " + str);
      System.out.println("The beginning of given String is: ");
      // loop to print beginning of the given string
      for(int i = 0; i < 2; i++) {
         System.out.print(strChars[i]);
      }
   }
}

Output

The given String is: Tutorials Point
The beginning of given String is: 
Tu

Example 2

We can also use the while loop to print the beginning of a given string. In the same code of previous example, instead of for loop, we will use the while loop to check the beginning of string.

public class Example2 {
   public static void main(String []args) {
      // initializing the string 
      String str = "Tutorials Point";
      // converting the string into character array
      char strChars[] = str.toCharArray();
      System.out.println("The given String is: " + str);
      System.out.println("The beginning of given String is: ");
      // loop to print beginning of the given string
      int i = 0;
      while( i < 2 ) {
         System.out.print(strChars[i]);
         i++;
      }
   }
}

Output

The given String is: Tutorials Point
The beginning of given String is: 
Tu

Example 3

The following example illustrates how we can use the charAt() method to check the beginning of a string. The charAt() method accepts index number as an argument and returns the character of string present at the specified position.

public class Example3 {
   public static void main(String []args) {
      // initializing the string 
      String str = "Tutorials Point";
      System.out.println("The given String is: " + str);
      // printing the beginning of string
      System.out.println("The given String begins with: " + str.charAt(0));
   }
}

Output

The given String is: Tutorials Point
The given String begins with: T

Example 4

This is another Java program that demonstrates how to check the beginning of the given string. We will use the substring() method that accepts the beginning and ending index as arguments and returns the characters available in between those indices.

public class Example4 {
   public static void main(String []args) {
      // initializing the string 
      String str = "Tutorials Point";
      System.out.println("The given String is: " + str);
      // printing the beginning of string
      System.out.println("The given String begins with: " + str.substring(0, 2));
   }
}

Output

The given String is: Tutorials Point
The given String begins with: Tu

Conclusion

We started this article with the definition of string and in the next section, we learned how to check the beginning of a given string. During our discussion on the problem statement, we discovered that we can use four different ways to find the beginning of a string namely for loop, while loop, charAt() method and substring() method.

Updated on: 10-Aug-2023

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements