Technology Programming

How to Remove a Substring From the String in Java

    • 1). Launch the program you use to edit Java programs.

    • 2). Open one of your Java files and locate the "Main" method.

    • 3). Paste the following code at the top of that method:

      String originalString = "This is the original text string";

      String subStringToRemove = "original text";

      String replacementValue = "";

      String blank = " ";

      The variable "originalString" holds your original text string. The next variable, "subStringToRemove," contains the substring you want to find. The "replacementValue" variable has the replacement string, and "blank" defines a blank character.

    • 4). Add the following code below the code in Step 3:

      String newString = originalString.replace(subStringToRemove + blank, replacementValue);

      System.out.println(newString);

      The first line executes Java's "replace" method. This method, a member of the "String" class, allows you to replace characters and words within a string. To get this method to remove a substring, the code assigns a null value to the "replacementValue" variable as shown in Step 3. The "blank" variable insures that no extra spaces appear in the resulting string. The variable "newString" holds that result. The last statement shows how the original string looks once Java removes the substring.

    • 5). Save the file and run it. The code will execute and remove "original text" from the original text string that contains "This is the original text string." The resulting string reads "This is the string."

Related posts "Technology : Programming"

The Importance Of Having a WordPress Business Theme

Programming

Website Design Is Necessary For Your Website

Programming

The Most effective On line Paid Survey Evaluation

Programming

Adelaide SEO - Links And Keywords, How Should They Be Used

Programming

C Programming Compilers for Microcontrollers

Programming

How Should A DJ Make Music Logo That Is Distinct And Cool?

Programming

Call to Action Concepts for Small Businesses

Programming

Microsoft Access Databases in Office 365

Programming

Why web design is crucial for producing world class websites

Programming

Leave a Comment