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

How to remove html tags from string java

  • If you want to remove html tags from a string in java you need to use regex.
  • Using java regex we can trim/remove html tags from given string.
  • Lets see a java example program on how can we remove html code or html tags from a string in java.
  • Input String : <B>hello</B>
  • output: hello



#1: Java Example program to remove html tags from a string

  1. package com.instanceofjava.removehtmltags;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: java convert html to plain text/ trim html code from string in java
  7.  *
  8.  */
  9. public class RemoveHtmlTags {

  10. public static void main(String[] args) {
  11. String htmlStr = "<p>Java Program to remove html tags from a String</p>";
  12.         System.out.println(htmlStr);
  13.         htmlStr = htmlStr.replaceAll("\\<.*?\\>", "");
  14.         System.out.println(htmlStr);
  15. }

  16. }

Output:
  1. <p>Java Program to remove html tags from a String</p>
  2. Java Program to remove html tags from a String

remove html code from string in java

How to get first two characters of a string in java

  • We can get first  two character of a string in java by using subString() method in java
  • To get first N numbers of a string s.substring(0,n);
  • Lets see an example java program on how to get first two characters or first N characters.
In Java, you can use the charAt() method to get the first character of a string. The charAt() method takes an index as a parameter and returns the character at that index. 
To get the first character of a string, you can pass in the index 0 to the charAt() method.

Here's an example:


String str = "Hello, World!";
char firstChar = str.charAt(0);
System.out.println("First character: " + firstChar);

This will output:
First character: H



Program #1: Java Program to get first two characters of a String.



  1.  package StringInterviewprograms;
  2.  
  3. public class GetFirstTwoCharacters {
  4.  
  5. /**
  6. * How to get First N characters in java 
  7. * @author www.instanceofjava.com
  8. */
  9.  
  10. public static void main(String[] args) {
  11.         
  12.         String str="Get first two characters of a String";
  13.         
  14.         System.out.println(str.substring(0,2));
  15. }
  16.     
  17.     
  18. }
     
Output:

  1.  Ge

  Program #2:


  1. package StringInterviewprograms;
  2. import java.util.Scanner;
  3.  
  4. public class GetFirstTwoCharacters {
  5. /**
  6. * How to get First N characters in java 
  7. * @author www.instanceofjava.com
  8. */
  9. public static void main(String[] args) {
  10.         
  11.          Scanner sc= new Scanner(System.in);
  12.          
  13.          System.out.println("Please Enter a String");
  14.          
  15.          String str=sc.next();         
  16.         if(str.length()>=2){
  17.  
  18.             System.out.println(str.substring(0,2));
  19.         }else{
  20.             System.out.println("please enter a string with minimum length of 2.");
  21.         }
  22.         }
  23.  
  24.   }

 Output:


  1. Please Enter a String
  2. miss you
  3. mi


Get First Two characters String Java

How to check if a character is a special character in java

  • To check if string contains special character or not or  check whether first character is special character or not we can use regex or contains method of sting class.
  • By using  java.util.regex.Matcher class and java.util.regex.Pattern class methods we can check whether string has special character or not
  • Lets see an example program on how to check a string has special character or not using java regex.



Program #1: Java example program to check string has special character or not using  java regex's?
 
  1. package StringInterviewprograms;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class CheckSpecialCharacterString {
  6.  
  7.     /**
  8.      * Check whether the String contains special character or not using java
  9.      * @author www.instanceofjava.com
  10.      */
  11.     
  12. public static void main(String[] args) {
  13.  
  14. String Str="Java String interview questions ";
  15. String Str1="Java String interview questions % ";
  16. Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
  17. Matcher m = p.matcher(Str);
  18. boolean check = m.find();
  19.  
  20. if (check)
  21.    System.out.println("String: "+Str+" contains special character");
  22. else
  23.    System.out.println("String: "+Str+" does not contains any special character");
  24.  
  25. Matcher m1= p.matcher(Str1);
  26.  
  27. boolean flag = m1.find();
  28.  
  29. if(flag)
  30.        System.out.println("String: "+Str1+" contains special character");
  31.     else
  32.        System.out.println("String: "+Str1+" does not contains any special character");
  33.  
  34.    }
  35. }
Output:

  1. String: Java String interview questions  does not contains any special character
  2. String: Java String interview questions %  contains special character

  • We can check each character of a string is special character or not without using java regex's.
  • By using String class contains method and for loop we can check each character of a sting is special character or not.
  • Let us see a java example program on how to check each character (including space) of a string is special character or not.
  • String validation for special characters.


Program #2: Java example program to check each character of string is special character or not without using  java regex's?

  1. package StringInterviewprograms;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class CheckSpecialCharacterString {
  7.  
  8.     /**
  9.      * Check whether the each character of String is special character or not using java
  10.      * @author www.instanceofjava.com
  11.      */
  12.     
  13. public static void main(String[] args) {
  14. String Str="Java String interview questions*$%";
  15.   
  16. String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";
  17.  
  18. for (int i = 0; i < Str.length(); i++) {
  19.     
  20.     if (specialCharacters.contains(Character.toString(Str.charAt(i))))
  21.     {
  22.        
  23.         System.out.println(Str.charAt(i)+": is a special character");
  24.     }  
  25.   }
  26.  
  27. }
  28.  
  29. }

Output:


  1.  : is a special character
  2.  : is a special character
  3.  : is a special character
  4. *: is a special character
  5. $: is a special character
  6. %: is a special character

Program #3: Java example program to check each character of string is special character or not without using  java regex's Using Eclipse IDE.


Check Special Character String java

How to check if first character of a string is a number or not in java

  • To check if first character of a string is a number or not in java we can check in multiple ways.
  • By using Character.isDigit() method by passing character of string using for loop or substring method.
  • Other way is to check str.substring(0, 1).matches("[0-9]").
  • Lets see an example program in java to check first character of a string is digit or character. 



Program #1: Java example program to check the each character of string is number or not   character is a letter or number in Java without using regexes?

  1. package StringInterviewprograms;
  2. /**
  3.  * Check whether the given character is a number /digit or not by using java character isdigit
  4.  *method
  5.  * @author www.instanceofjava.com
  6.  */
  7. public class CheckEachCharacterNumberOrNot {
  8.     public static void main(String[] args) {
  9.         String str = "instanceofjava37";
  10.  
  11.         //Check whether the given character is a number /digit or not ?
  12.  
  13.         for(int i=0; i<str.length();i++)
  14.         {
  15.             Boolean flag = Character.isDigit(str.charAt(i));
  16.  
  17.             if(flag){
  18.  
  19.                System.out.println("'"+str.charAt(i)+"' is a number");
  20.             }
  21.             else{
  22.                System.out.println("'"+str.charAt(i)+"' is not a number");
  23.             }
  24.  
  25.         }
  26.         
  27. }
  28.  
  29. }

Output:

  1. 'i' is not a number
  2. 'n' is not a number
  3. 's' is not a number
  4. 't' is not a number
  5. 'a' is not a number
  6. 'n' is not a number
  7. 'c' is not a number
  8. 'e' is not a number
  9. 'o' is not a number
  10. 'f' is not a number
  11. 'j' is not a number
  12. 'a' is not a number
  13. 'v' is not a number
  14. 'a' is not a number
  15. '3' is a number
  16. '7' is a number
     
Program #2: Java example program to check the First character of string is number or not by using regex.

  1. package StringInterviewprograms;
  2. /**
  3.  * Check whether the given character is a number /digit or not
  4.  * @author www.instanceofjava.com
  5.  */
  6. public class CheckFirstCharacterNumberOrNot {
  7.  
  8.     public static void main(String[] args) {
  9.         String str = "instanceofjava37";
  10.  
  11.           Boolean flag1 = str.substring(0, 1).matches("[0-9]");
  12.  
  13. if(flag1){
  14.  
  15.  System.out.println("First Character is a number..!");
  16.  
  17.  }
  18.  else{
  19.    System.out.println("First character is not a number..!");
  20. }
  21. }
  22.  
  23. }

Output:

  1. First character is not a number..!


Program #3: Java example program to check the First character of string is number or not using eclipse IDE.


Check first character string number digit java

How to find uppercase letters in a string in java

  • How can we count upper case letter in String  java ?
  • How to find uppercase letters in String  java?
  • How to find capital letters in string in java?
  • How u discover the capital letter given sentence?
  • Yes We can find uppercase or capital letters in a String using isUpperCase() method of Character class in java
  • In order to find number of uppercase letters in a string of all capital letters in a string we need to iterate all characters of a string in a loop and check individual characters are uppercase letters or not using Character class provided isUpperCase() method.
  • To find the uppercase letters in a string in Java, you can use the isUpperCase() method of the Character class.




Program #1: Java example program to find all capital letters / Uppercase letters in a String


  1. package findUppercaseletters.String;
  2. public class FinfUppercaseLetters {
  3.  
  4.     /**
  5.      * @website: www.instanceofjava.com
  6.      */
  7.  
  8.  public static void main(String[] args) {
  9.         
  10.         
  11.   String str= "How to Print Uppercase Letters in Java";
  12.  
  13.     for (int i = 0; i < str.length(); i++) {
  14.     
  15.             if(Character.isUpperCase(str.charAt(i))){    
  16.             System.out.println(str.charAt(i));
  17.             }
  18.             
  19.  }
  20.  
  21. }
  22.  
  23. }

 Output:


  1. H
  2. P
  3. U
  4. L
  5. J
  • Above code will print all the uppercase letters in the string.
  • Alternatively, you can also use the toUpperCase() method of the Character class to convert all the characters in the string to uppercase and then use the equals() method to compare them to the original character.
Program #2: Java example program to count number of upperCase letters in a String


how to find uppercase letters in a string in java

Select Menu