×

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax
public boolean endsWith(String Suffix)
Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current String is matched with the given suffix. Otherwise, it returns false Java String endsWith() Example 1:
public class JavaStringEndsWithEx1
{ 
   public static void main(String args[])
  { 
   String str1,str2;
   str1="welcome to java tutorial";
   str2=str1.concat(" and example");
   System.out.println(str2.endsWith("ample"));
   System.out.println(str1.endsWith("al"));
   System.out.println(str1.endsWith("exampale"));
   }  
}
Output:
true
true
false
Java String endsWith() Example 2:
public class JavaStringEndsWithEx2
    { 
        public static void main(String[] args)
        { 
            String str = "tutorialandexample";
            System.out.println(str.endsWith("and")); 
            if(str.endsWith("example")) { 
                String str2=str.concat(".com");
                System.out.println(str2);
            }else
               System.out.println("something else"); 
        } 
    }
Output:
 false
 tutorialandexample.com
Java String endsWith() Example 3:
    public class JavaStringEndsWithEx3    { 
        public static void main(String[] args)
        {
            String[] s1 = {"welcome","to","java","tutorial"};
            System.out.println(s1[0]);
            System.out.println(s1[1]);
          //retriveing the array strings
            for (String element:s1){
            if ( element.endsWith("a")) {
               System.out.println("java");
              }else if( element.endsWith("l")){
               System.out.println("tutorial"); 
              }
            } 
      }
}

Output:
welcome
to
java
tutorial

Related Topics

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.