Found 7442 Articles for Java

Write a java program reverse tOGGLE each word in the string?

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

829 Views

To perform a reverse toggle split the words of a string, reverse each word using the split() method, change the first letter of each word to lower case and remaining letters to upper case.Example Live Demoimport java.lang.StringBuffer; public class ToggleReverse {    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          StringBuffer s = new StringBuffer(word);          word = s.reverse().toString();          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputoLLEH wOH eRA uOY

Constructors of StringBuffer class in Java.

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

1K+ Views

Following are the various constructors provided by the StringBuffer class.S.N.Constructor & Description1StringBuffer()This constructs a string buffer with no characters in it and an initial capacity of 16 characters.2StringBuffer(CharSequence seq)This constructs a string buffer that contains the same characters as the specified CharSequence.3StringBuffer(int capacity)This constructs a string buffer with no characters in it and the specified initial capacity.4StringBuffer(String str)This constructs a string buffer initialized to the contents of the specified string.

Check if a string contains a number using Java.

Anjana
Updated on 06-Sep-2023 14:07:32

41K+ Views

To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.ExampleLive Demopublic class ContainsExample {    public static void main(String args[]){       String sample = "krishna64";       char[] chars = sample.toCharArray();       StringBuilder sb = new StringBuilder();       for(char c : chars){          if(Character.isDigit(c)){             sb.append(c);          }       }       System.out.println(sb);    } }Output64

Write a java program to tOGGLE each word in string?

Alankritha Ammu
Updated on 26-Feb-2020 07:07:50

1K+ Views

You can change the cases of the letters of a word using toUpperCase() and toLowerCase() methods.Split each word in the string using the split() method, change the first letter of each word to lower case and remaining letters to upper case.ExampleLive Demopublic class Sample{    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputhELLO hOW aRE yOU

How to test if a string contains specific words in Java?

Alankritha Ammu
Updated on 26-Feb-2020 09:56:55

3K+ Views

The java.lang.String.contains() method returns true if and only if this string contains the specified sequence of char values.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.contains(test);       System.out.println(bool);    } }Outputtrue

Write a java program to reverse each word in string?

Anjana
Updated on 26-Feb-2020 07:05:44

816 Views

StringBuffer class of the java.lang package provides reverse() method. This method returns a reverse sequence of the characters in the current String. Using this method you can reverse a string in Java.To reverse each word in a string you need to split the string, store it in an array of strings and reverse each word using the reverse() method of the StringBuffer class.ExampleLive Demoimport java.lang.*; public class StringBufferDemo {    public static void main(String[] args) {       StringBuffer buff = new StringBuffer("tutorials point");       System.out.println("buffer = " + buff);       // reverse characters of the ... Read More

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

231 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

176 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

Advertisements