Create a String from a Java Array

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

436 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

Extract Substring from a String in Java

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

10K+ 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

Extract Last N Characters from a String in Java

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

12K+ 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

Extract First N Characters from a String in Java

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

870 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

Count Characters in a Java String

Vikyath Ram
Updated on 20-Feb-2020 04:43:25

802 Views

Declare an integer, initialize it with 0, in for loop increment it for each character.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       String str = new String("Hi welcome to Tutorialspoint");       int count = 0;       for(int i = 0; i

Concatenate Two Strings in Java

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

352 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

Purpose of toString Method in Java

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

242 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

Difference Between String s1 = "Hello" and String s1 = new String("Hello") in Java

Rishi Raj
Updated on 19-Feb-2020 12:48:04

3K+ Views

When you store a String asString str1 = "Hello";directly, then JVM creates a String object with the given value in a separate block of memory known as String constant pool.And whenever we try to create another String asString str2 = "Hello";JVM verifies whether any String object with the same value exists in the String constant pool, if so, instead of creating a new object JVM assigns the reference of the existing object to the new variable.And when we store String asString str = new String("Hello");using the new keyword, a new object with the given value is created irrespective of the ... Read More

Modifier Volatile in Java

Jai Janardhan
Updated on 19-Feb-2020 12:41:26

636 Views

The volatile modifier is used to let the JVM understand that a thread accessing the variable should always merge its own personal copy of the variable with the original in the memory.Accessing a volatile variable synchronizes all the cached copy of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.Examplepublic class MyRunnable implements Runnable {    private volatile boolean active;    public void run() {       active = true;       while (active) {           }    }    public void stop() {       active = false;      } }

Create and Write JSON Array to a File in Java

Abhinaya
Updated on 19-Feb-2020 12:26:38

4K+ Views

Java provides javax.json.Json package which contains classes to read a JSON array:Exampleimport java.io.FileOutputStream; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonWriter; public class JSONArrayToFile {    public static void main(String args[]) throws Exception {       JsonArray value = Json.createArrayBuilder()          .add(Json.createObjectBuilder()          .add("id", "1001")          .add("Technology", "JavaFX"))          .add(Json.createObjectBuilder()          .add("id", "1002")          .add("Technology", "OpenCV"))          .build();       System.out.println(value);       JsonWriter writer = Json.createWriter(new FileOutputStream("sampleData"));       writer.writeArray(value);       writer.close();    } }Output["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]

Advertisements