How To Remove Character From a String Java

How To Remove Character From a String Java | To remove a character from a string we use several predefined functions available in Java.

How To Remove First Character From String In Java

To remove the first character from the string we can use the substring() method of the String class. In the main method, we have initialized the string as “Know Program” and by using the substring() method we remove the first character.

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      System.out.print("String after removing the first character: ");
      System.out.println(string.substring(1));
   }
}

Output:

String after removing the first character: now Program

Java Remove Last Character From String

To remove the last character in the string we can use the StringBuilder/StringBuffer class, which creates a mutable string. Both classes have deleteCharAt() method which deletes the character at a specified length.

public class Main {
   public static void main(String args[]) {
      String string = "Know Program";
      StringBuilder sb = new StringBuilder(string);
      sb.deleteCharAt(sb.length() - 1);
      System.out.println(sb);
   }
}

Output:

Know Progra

Java String Remove Character At Index

In this section, we will remove a character from the string at the specified index. For this purpose, we can use deleteCharAt() method of the StringBuilder or StringBuffer class to remove the character from the given index.

class Main {
   public static void main(String[] args) {
      StringBuilder str = new StringBuilder("Know Program");
      System.out.println("Actual String = " + str.toString());

      str = str.deleteCharAt(3);
      System.out.print("String after removing the character: ");
      System.out.println(str.toString());
   }
}

Output:-

Actual String = Know Program
String after removing the character: Kno Program

Remove Character From String in Java Without Using Inbuilt function

We try to remove the characters in a string without using any inbuilt function.

First, we initialize string str to value “Know Program” then we take an integer value “I” and initialize it to 0 by using while loop we check for the string length and convert the string to the character by using charAt() method. If we specify the character to be removed and remove the particular character.

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      int i = 0;
      while (i < string.length()) {
         char ch = string.charAt(i);
         // remove 'g' from the string 
         if (ch == 'g') {
            String s1 = string.substring(0, i);
            String s2 = string.substring(i + 1);
            string = s1 + s2;
         } else {
            i++;
         }
      }
      System.out.println(string);
   }
}

Output:

Know Proram

Remove Multiple Characters From String Java

In this example, we will remove multiple characters from a string. We can do this by using the substring() method. This operation can be performed by specifying the characters needed to be removed in the if loop. In the below code, we have removed characters ‘g’, ‘r’, and ‘o’ from the string “Know Program”.

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      int i = 0;
      while (i < string.length()) {
         char ch = string.charAt(i);
         if (ch == 'g' || ch == 'r' || ch == 'o') {
            String s1 = string.substring(0, i);
            String s2 = string.substring(i + 1);
            string = s1 + s2;
         } else {
            i++;
         }
      }
      System.out.println(string);
   }
}

Output:

Knw Pam

Java Remove Characters From String Regex

Regex stands for the regular expression. Java provides a class called regular expression to specify the characters directly. In this code, we use regex to remove characters from the string. Note that regular expression is case-sensitive.

public class Main {
   public static void main(String[] args) {
      String str = "Know Program";
      System.out.println("Actual String = " + str.toString());

      str = str.replaceAll("[Kro]", "");
      System.out.println(str.toString());
   }
}

Output:-

Actual String = Know Program
nw Pgam

In the String “KnowProgram”, characters ‘K’, ‘r’, and ‘o’ will be replaced with “” (empty). It means they will be removed from the string.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *