Found 33676 Articles for Programming

How to check palindrome string in java?

Ayyan
Updated on 20-Nov-2024 22:41:36

19K+ Views

In this article, we will learn how to check whether a string is a palindrome in Java. A palindrome reads the same forward and backward. By using the StringBuffer class and its reverse() method. Problem StatementGiven a string, write a Java program to check if it is a palindrome by reversing the string and comparing it with the original.Input "anna"Output Given String is palindrome Steps to check if a string is a palindrome The following are the steps to check if a string is a palindrome − Create a StringBuffer object by passing the ... Read More

How to check if a String contains another String in a case insensitive manner in Java?

Alankritha Ammu
Updated on 26-Feb-2020 09:59:12

2K+ Views

One way to do it to convert both strings to lower or upper case using toLowerCase() or toUpperCase() methods and test.ExampleLive Demopublic class Sample {    public static void main(String args[]){       String str = "Hello how are you welcome to Tutorialspoint";       String test = "tutorialspoint";       Boolean bool = str.toLowerCase().contains(test.toLowerCase());       System.out.println(bool);    } }Outputtrue

Java String Contains Example.

Anjana
Updated on 26-Feb-2020 09:40:10

234 Views

The contains() method of String class returns true if and only if this string contains the specified sequence of char values.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials point", str2 = "http://";       CharSequence cs1 = "int";       // string contains the specified sequence of char values       boolean retval = str1.contains(cs1);       System.out.println("Method returns : " + retval);       // string does not contain the specified sequence of char value       retval = str2.contains("_");       System.out.println("Methods returns: " + retval);    } }OutputMethod returns : true Methods returns: false

How does the Java compareTo() method, compare strings?

Manikanth Mani
Updated on 26-Feb-2020 09:41:58

177 Views

The compareTo() method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc.However, two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type.ExampleLive Demopublic class Test {    public static void main(String args[]) {       Integer x = 5;       System.out.println(x.compareTo(3));       System.out.println(x.compareTo(5));       System.out.println(x.compareTo(8));    } }Output1 0 -1

Java Program to check whether one String is a rotation of another.

Ayyan
Updated on 30-Jul-2019 22:30:21

529 Views

To find weather a string is rotation of another, concat the first string to itself twice and find weatherExample Live Demopublic class Sample {    public static void main(String args[]){       String str1 = "gala";       String str2 = "alag";       String s3 = str1+str1;       if(s3.contains(str2)) {          System.out.println("str1 is rotation of str2");       } else {          System.out.println("str1 is not rotation of str2");       }    } }

Write a Java program to capitalize each word in the string?

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

2K+ Views

You can capitalize words in a string using the toUpperCase() method of the String class. This method converts the contents of the current string to Upper case letters.Example Live Demopublic class StringToUpperCaseEmp {    public static void main(String[] args) {       String str = "string abc touppercase ";       String strUpper = str.toUpperCase();       System.out.println("Original String: " + str);       System.out.println("String changed to upper case: " + strUpper);    } }OutputOriginal String: string abc touppercase String changed to upper case: STRING ABC TOUPPERCASE

How to remove all whitespace from String in Java?

Aishwarya Naglot
Updated on 24-Jun-2025 13:03:06

8K+ Views

In this article, we will learn how to remove all whitespace from a string in Java. For example, if the given string contains white spaces as shown below - Initial String = "Hi how are you Welcome to Tutorialspoint.com" The output should contain a string without white spaces as - String without whitespaces = "HihowareyouWelcometoTutorialspoint.com" Using replaceAll() Method The replaceAll() method replaces substrings that match a given regular expression with a specified string. By passing white spaces and an empty string as parameters to this method, we can remove all whitespaces from the input string. Steps to remove ... Read More

Difference between charAt() and indexOf() in Java ?

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

3K+ Views

The charAt() method of the String class returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.The indexOf(int ch, int fromIndex) method of the String class returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of ... Read More

How to convert a Java String to an Int?

Alankritha Ammu
Updated on 30-Jul-2019 22:30:21

352 Views

The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.Example Live Demopublic class Test {    public static void main(String args[]) {       int x =Integer.parseInt("9");       double c = Double.parseDouble("5");       int b = Integer.parseInt("444",16);       System.out.println(x);       System.out.println(c);       System.out.println(b);    } }Output9 5.0 1092

Checking for Null or Empty or White Space Only String in Java.

Anjana
Updated on 30-Jul-2019 22:30:21

2K+ Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       // prints length of string       System.out.println("length of string = " + str.length());       // checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

Advertisements