Java remove character from string

Hello everyone!! Welcome to flower brackets blog. Well, in the following example let us learn java remove character from string.

java remove character from string

Java remove character from string

While designing software using java we often come across a phase where we need to an extra bit of work.

Also Read – Learn Strings In Java

In java, String class has replace methods with different overloaded parameters as follows,

replace(char oldChar, char newChar)
returns a string from replacing all occurrences of oldChar with newChar.

replace(CharSequence target, CharSequence replacement) 
replace substring of string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst(String regex, String replacement)
replace the first substring of given string that matches the given regular expression with the given replacement.

replaceAll(String regex, String replacement)
replace each substring of this string that matches the given regular expression with the given replacement.

Example

Here in this example I have created a separate method removeCharacter with two parameters namely strRemove and position.

Let’s see the code for remove character from string in java,

public class RemoveStringChar
{
   public static void main(String[] args)
   {
      String strGiven = "Core Java";
      System.out.println("Before removing string character - " + strGiven);
      System.out.println("After removing string character - " + removeCharacter(strGiven, 3));
   }
 
   public static String removeCharacter(String strRemove, int position)
   {
      return strRemove.substring(0, position) + strRemove.substring(position + 1); 
   }
}

Output:

java remove character from string


replace method

In this example we are going to remove a character from a string in java using replace function,

public class StringRemoveJava
{
   public static void main(String[] args)
   {
      String strExample = "jdetyJDETY551857";
      String strOutput = strExample.replace("d", "q");
      System.out.println("String remove character : " + strOutput);
   }
}

Output:

String remove character : jqetyJDETY551857


replaceFirst method

Now let’s learn removing characters from a string java using replaceFirst method,

public class RemoveString
{
   public static void main(String[] args)
   {
      String strExample = "jdetyJDETY551857";
      String strOutput = strExample.replaceFirst("jd", "xy");
      System.out.println("Java remove a character from a string : " + strOutput);
   }
}

Output:

Java remove a character from a string : xyetyJDETY551857


replaceAll method

In this example we learn remove characters from string using replaceAll method,

public class StringRemoveJava
{
   public static void main(String[] args)
   {
      String strExample = "George hates samantha";    
      System.out.println("Before : " + strExample);
      strExample = strExample.replaceAll("hates", "loves");    
      System.out.println("After : " + strExample);
   }
}

Output:

Before : George hates samantha
After : George loves samantha


Remove only letters

In this example I have a string with numeric values and alphabet values. What if we wanted to remove all the letters from the given string.

public class RemoveString
{
   public static void main(String[] args)
   {
      String strExample = "jd15352kLJJD55185716kdkLJJJ";   
      System.out.println("Before : " + strExample);
      strExample = strExample.replaceAll("[a-zA-Z]", "");   
      System.out.println("After : " + strExample);
   }
}

Output:

Before : jd15352kLJJD55185716kdkLJJJ
After : 1535255185716

Now all we have is just the numbers. If you want to see the space between letters. Just give space between double quotes like this,

strExample = strExample.replaceAll("[a-zA-Z]", " ");

And the output will be like this,

Before : jd15352kLJJD55185716kdkLJJJ
After :    15352     55185716


Using StringBuffer – java remove last character from string

Meanwhile in java we can convert String to StringBuffer and then use remove() method to remove first or last character.

In StringBuffer class we have deleteCharAt(int index) method. This method removes a character from corresponding index.

Also Read – String Length Java

deleteCharAt(int index) method can be used to remove first and last character from string in java.

Now let’s see an example on how to remove the first character of a string in java,

using substring()

In java we have two methods in java.lang.String class they are,

  • substring(int beginIndex)
  • substring(int beginIndex, int endIndex)

substring(int beginIndex) : returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

For example :

public class SubString 
{
   public static void main(String[] args) 
   {
      String demo = "helloworld";
      String result = demo.substring(5);
      System.out.println("Remove first character from string : " + result);
   }
}

Output :

Remove first character from string : world


substring(int beginIndex, int endIndex) : returns a new string which is a substring of this string. substring begins at specified beginIndex and extends to the character at index endIndex -1.

public class SubString 
{
   public static void main(String[] args) 
   {
      String demo = "helloworld";
      String result = demo.substring(3, 6);
      System.out.println("Java remove first and last character from string : " + result);
   }
}

Output :

Java remove first and last character from string : low

Here you can see string “helloworld”, beginning index is 0 and end index is 9. Here I am calling substring method and I am passing beginIndex as 3 and endIndex is 6.

In this method beginIndex is inclusive and endIndex is exclusive. So we will get “low”.


Let’s see stringbuilder remove last character in java using delete method of StringBuilder class.

public StringBuilder delete(int start, int end) : removes the characters in a substring of this sequence. 

The substring begins at the specified start and extends to the character at index end -1 or to the end of the sequence if no such characters exists. 

If start is equal to end, no changes are made.

public class StringBuilderExample 
{
   public static void main(String[] args) 
   {
      StringBuilder sb = new StringBuilder("helloworld");
      System.out.println("Stringbuilder delete last character, before : " + sb);
      sb = sb.delete(3, 6);
      System.out.println("Stringbuilder delete last character, after : " + sb);
   }
}

Output :

Stringbuilder delete last character, before : helloworld
Stringbuilder delete last character, after : helorld

Here I have declared StringBuilder with string “helloworld”. Then I am calling delete method of StringBuilder.

Also Read – Sort String Array Java

I am passing start index 3 which is inclusive and end index 6 which is exclusive. This delete method will delete the substring “low”.

So the output will be “helorld”.


Another way to remove last character from string is by using deleteCharAt(int index) method

public StringBuilder deleteCharAt(int index) : removes the char at the specified position in this sequence. This sequence is shortened by one char

public class StringBuilderExample 
{
   public static void main(String[] args) 
   {
      StringBuilder sb = new StringBuilder("helloworld");
      System.out.println("Stringbuilder delete last character, before : " + sb);
      sb = sb.deleteCharAt(3);
      System.out.println("Stringbuilder delete last character, after : " + sb);
   }
}

Output :

Stringbuilder delete last character, before : helloworld
Stringbuilder delete last character, after : heloworld

Here I have declared StringBuilder with string “helloworld”. Then I am calling deleteCharAt method of StringBuilder and passing index 3.

In index position 3 we have “l”. This method will remove character at 3rd index position which is “l”.

So the output will be “heloworld”.


conclusion

So this was all about java remove character from string. You also learnt how to remove the first character of a string in java, stringbuilder remove last character java and many more.

I hope you have understood the concept of java remove character from string. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts