Posts

Showing posts with the label String

How to replace a character at a specific index in a String in Java

In this section, we will show you how replace a character at a specific index in a String in Java. Note: String is an immutable class in java.  That means we cannot make any change in the String object.  Any method which seems to modify it always returns a new string object with modification.  Following ways can be used to replace a character at a specific index in a String in Java: By u sing substring () method By using StringBuilder setCharAt() method By using StringBuffer setCharAt() method By using toCharArray() method Example 1: Using substring () method Create a new string with the character replaced. public class Main { // Driver code public static void main ( String [] args) { // Get the String String string = "How ate you?" ; // Get the index int index = 5 ; // Get the character char ch = 'r' ; // Print the original string System . out .println( "Original String = " + s

Java - Convert colon-separated String to a List and List to a colon-separated String

In this section, we will show you h ow to c onvert colon-separated String to a List and  vice versa . 1. Colon-separated String to List Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { String str = "India: USA: Japan: Russia" ; //Remove whitespace and split by colon List < String > result = Arrays . asList ( str .split( " \\ s*: \\ s* " )); System . out .println( result ); } } Console Output: [India, USA, Japan, Russia] 2. List to Colon-separated String Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "India" , "USA" , "Japan" , "Russia" ); String result = String . join ( ":" , list ); System . out .println( result ); } } Console O

Java - Convert comma-separated String to a List and List to a comma-separated String

In this section, we will show you h ow to c onvert comma-separated String to a List and  vice versa . 1. Comma-separated String to List Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { String str = "India,USA,Japan,Russia" ; //Remove whitespace and split by comma List < String > result = Arrays . asList ( str .split( " \\ s*, \\ s* " )); System . out .println( result ); } } Console Output: [India, USA, Japan, Russia] 2. List to Comma-separated String Main.java import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "India" , "USA" , "Japan" , "Russia" ); String result = String . join ( "," , list ); System . out .println( result ); } } Console Outp

How to search a String in a List in Java?

In this section, we will show you h ow to search a string in a List in Java. We can use  .contains() , . startsWith() or .matches() to search for a string in ArrayList. Java 8 Example import java.util.ArrayList ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = new ArrayList<>(); list .add( "India" ); list .add( "USA" ); list .add( "Japan" ); list .add( "Brazil" ); list .add( "Israel" ); //.contains example List < String > result1 = list .stream() .filter(x -> x.contains( "India" )) .collect( Collectors . toList ()); System . out .println( result1 ); //.startsWith example List < String > result2 = list .stream() .filter(x -> x.st

How to Convert Optional to String in Java 8

In this section, we will show you h ow to convert Optional<String> to String. Samples A standard way to get a value. import java.util.Arrays ; import java.util.List ; import java.util.Optional ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "a" , "i" , "h" , "d" , "e" ); Optional < String > result = list .stream() .filter(x -> x.length() == 1 ) .findFirst(); if ( result .isPresent()) { System . out .println( result .get()); // a } } } Console Output: a use .map(Object::toString) to convert an Optional<String> to a String . import java.util.Arrays ; import java.util.List ; import java.util.Optional ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "a" , "i" , "h" ,

How to convert Char to String in Java

In this section, we will show you h ow to convert Char to String in Java. Main.java public class Main { public static void main ( String [] args) { String str = "knowledgefactory.net" ; //convert a String into char char charK = str .charAt( 0 ); //k char charL = str .charAt( 4 ); //l char charC = str .charAt( 11 ); //c System . out .println( charK ); System . out .println( charL ); System . out .println( charC ); //convert char back to String String temp = Character . toString ( charK ); System . out .println( temp ); } } Console Output: k l c k How to calculate days between two dates in Java 8? How to read a file in Java with BufferedReader Java 8 Streams - Count Frequency Of Words In a List Java 8 Streams - Program To Break A List Into Batches Of Given Size Remove duplicates from an array in Java Java Program To Convert String To HashMap Java 8 Stream - Remove duplicates from a l