Found 9326 Articles for Object Oriented Programming

How to create a string from a Java Array?

Arushi
Updated on 20-Feb-2020 04:49:12

311 Views

You can convert an array of Strings to a single array using the collect() method.ExampleLive Demoimport java.util.Arrays; import java.util.stream.Collectors; public class CreatngStringFromArray {    public static void main(String args[]) {       String [] myArray = {"Welcome", "to", "Tutorialspoint"};       String str = Arrays.stream(myArray).collect(Collectors.joining(" "));       System.out.println(str);    } }OutputWelcome to Tutorialspoint

How to extract certain substring from a string using Java?

Paul Richard
Updated on 20-Feb-2020 04:48:30

9K+ Views

You can extract a substring from a String using the substring() method of the String class to this method you need to pass the start and end indexes of the required substring.ExampleLive Demopublic class Substring {    public static void main(String args[]) {       String str = "Welcome to Tutorialspoint";       String sub = str.substring(10, 25);       System.out.println(sub);    } }OutputTutorialspoint

How to extract the last n characters from a string using Java?

Vikyath Ram
Updated on 20-Feb-2020 04:47:44

10K+ Views

To extract last n characters, simply print (length-n)th character to nth character using the charAt() method.ExampleLive Demopublic class ExtractingCharactersFromStrings {    public static void main(String args[]) {           String str = "Hi welcome to tutorialspoint";       int n = 5;       int initial = str.length()-5;       for(int i=initial; i

How to extract the first n characters from a string using Java?

Rishi Raj
Updated on 20-Feb-2020 04:46:52

609 Views

To find the consonants in the given String compare every character in it using the charAt() method with the vowel letters and remaining are consonants.ExampleLive Demopublic class FindingConsonants {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

How to find consonants in a given string using Java?

George John
Updated on 30-Jul-2019 22:30:20

2K+ Views

One way to find the vowels in a given String is to compare every character in it using the charAt() method with the vowel letters. Example Live Demo public class FindingConsonants { public static void main(String args[]) { String str = new String("Hi Welcome to Tutorialspoint"); for(int i=0; i

How to find the vowels in a given string using Java?

Arushi
Updated on 07-Oct-2023 02:01:20

26K+ Views

You can read a character in a String using the charAt() method. To find the vowels in a given String you need to compare every character in it with the vowel letters. Example Live Demo public class FindingVowels {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

How to concatenate two strings using Java?

Vikyath Ram
Updated on 19-Feb-2020 12:50:01

234 Views

You can concatenate two Strings using the concat() method.ExampleLive Demopublic class ConcatinatedStrings {    public static void main(String args[]) {             String str1 = new String("Tutorials");       String str2 = new String( "Point");       String res = str1.concat(str2);       System.out.println(res);    } }OutputTutorialsPoint

What is the difference between a String object and a StringBuffer object in java?

Rishi Raj
Updated on 30-Jul-2019 22:30:20

313 Views

String class is immutable once you create an object of string you can modify its data.The StringBuffer class is immutable, once you create a StringBuffer object you can change/modify the contents of it.This class provides various methods to manipulate its data such as append(), delete(), insert() etc.

What is the purpose of toString() method? Or What does the toString() method do in java?

George John
Updated on 19-Feb-2020 12:49:21

158 Views

The toString() method returns the current object in String format.ExampleLive Demopublic class Test { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.toString()); System.out.println("Hello"); } }OutputTest@2a139a55 Hello

What is the difference between a String object and a String literal in Java?

Paul Richard
Updated on 30-Jul-2019 22:30:20

726 Views

When the String literal used to create String, JVM initially checks weather String with the same value in the String constant pool, if available it creates another reference to it else it creates a new object and stores it in the String constant pool.In case of an object, each time you instantiate the class a new object is created given value irrespective of the contents of the String constant pool.

Advertisements