How to remove square brackets from string in java

  • Remove square brackets from string in java.
  • We can remove square brackets from string by  using regular expressions.
  • By using regular expressions we can remove any special characters from string.
  • Now , we will check how to remove brackets from a string using regular expressions in java.



#1: Java Example program to remove brackets from string using regex.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: remove square brackets from string 
  7.  *
  8.  */
  9. public class ArrayToString {

  10. public static void main(String[] args) {
  11.  String strbrackets = "[String nundi ][brackets][remove cheyyadam][yela?]";
  12.  strbrackets = strbrackets.replaceAll("\\[", "").replaceAll("\\]","");
  13.  System.out.println(strbrackets);
  14.  
  15. }
  16. }


Output:

  1. String nundi bracketsremove cheyyadamyela?


#2: Java program to remove curly brackets from string 



remove square brackets from string java curly

How to convert array to string without brackets in java

  • Converting array to string and string should not contain any brackets.
  • We can convert array to string by iterating array and capturing each element from an array and append to StringBuilder/StringBuffer so that final output will be string without brackets.
  • Lets see an example java program to convert array to string by removing brackets.
  • So that we can remove brackets from string.



#1 : Java Example program to convert array to string without brackets.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: convert array to string without brackets.
  7.  *
  8.  */
  9. public class ArrayToString {

  10. public static void main(String[] args) {
  11.  String array[]= {"java","string","without","brackets"};
  12.  StringBuffer strbuffer = new StringBuffer();
  13.  for (String str : array) {
  14.   strbuffer.append(str).append(" ");
  15.  }
  16.  String result = strbuffer.toString();
  17.  System.out.println(result);
  18.  
  19. }

  20. }

Output:

  1. java string without brackets 



convert array to string without brackets

Java Program to convert ArrayList to String array

  • Java code to convert arraylist to string array.
  • We can convert ArrayList of strings to String array by using  toArray() method.
  • Lets see an Example java program to convert ArrayList to string array.



#1:  Java example program to convert ArrayList to String Array

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = new String[lstflowers.size()];
  16. arrayflower = lstflowers.toArray(arrayflower);

  17. for(String flower : arrayflower)
  18.     System.out.println(flower);
  19. }

  20. }


Output:


  1. Rose
  2. Lilly

#2:  Java example program to convert ArrayList to String Array Using java 8

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array using java 8
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = lstflowers.toArray(new String[lstflowers.size()]);

  16. for(String flower : arrayflower)
  17.     System.out.println(flower);
  18. }

  19. }


java program to convert arraylist to string array

Java 8 subtract N minutes from current date

  • Java 8 provides java.time.LocalDateTime class.
  • By using minusMinutes() methods of LocalDateTime class we can subtract minutes from date or current date in java.
  • Lets see an example program on how to remove / subtract n minutes from current date using java 8.



  1. package com.instanceofjava.java8;
  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: subtract minutes to current date using java 8
  8.  *
  9.  */
  10. public class AddMinutesToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13.     LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before subtracting 30 minutes to date: "+datetime);
  15.     //add seconds by using minuesMinutes(seconds) method
  16. datetime=datetime.minusMinutes(30);
  17. System.out.println("After subtracting 30 minutes to date: "+datetime);

  18. }

  19. }

Output:

  1. Before subtracting 30 minutes to date: 2018-02-05T22:41:42.463
  2. After subtracting 30 minutes to date: 2018-02-05T22:11:42.463


subtract minutes from java 8 date time.png

How to Add N minutes to current date using java 8

  • Java 8 providing java java.time package which is having more utility methods related to date and time.
  • LocalDateTime class providing plusMinutes(int minutes) method to add minutes to current date or given date in java.
  • Lets see an example program to add 30 minutes to current date using java 8 LocalDateTime class.



#1: Java Example program to add 30 minutes to current date 

  1. package com.instanceofjava.java8;

  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: Add minutes to current date using java 8
  8.  *
  9.  */
  10. public class AddMinutesToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13.     LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before adding minutes to date: "+datetime);
  15.     //add seconds by using plusMinutes(Minutes) method
  16. datetime=datetime.plusMinutes(30);
  17. System.out.println("After adding 30 minutes to date: "+datetime);

  18. }

  19. }



Output:


  1. Before adding minutes to date: 2018-02-05T22:14:33.664
  2. After adding 30 minutes to date: 2018-02-05T22:44:33.664



add minutes to current date java 8


Can we define default methods in functional interfaces in java 8

  • Functional interfaces in java 8 will have single abstract method in it.
  • Check below two pages regarding defining and using functional interfaces using Lamda expressions.
  • Java 8 functional interface with example
  • Implement java 8 functional interface using lambda example program
  • We can define default methods in functional interfaces in java 8.
  • But every functional interface must have single abstract method in it.
  • As we know that default methods should have a implementation as they are not abstract methods.
  • Lets see an example program on defining functional interface with default method.



#1:  Java example program to create functional interface with default method.

  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {
  11.  
  12. void show();
  13.         //default method
  14. default void message() {
  15. System.out.println("hello this is default method in functional interface");
  16. }
  17. }


#2: Java Example program on how to implement functional interface abstract method and use default method.


Can we define default methods in functional interfaces in java 8


Select Menu