String-02. Delete the substring from the string

Source: Internet
Author: User

Enter two strings S1 and S2. Delete All substrings S2. that is, The result string cannot contain S2.

Input Format:

The input two non-empty strings of no more than 80 characters ending with a carriage return are provided in the two lines, corresponding to S1 and S2.

Output Format:

Output The result string after deleting all the substrings S2 in string S1 in one row.

Input example:

Tomcat is a male ccatatcat

Output example:Tom is a male

Import java. util. utility; public class main {public static void main (string [] ARGs)
{CIN = new partition (system. in); string STR = cin. nextline (); string STRC = cin. nextline (); string temp = STR; string result = Str. replaceall (STRC, ""); While (! Result. Equals (temp ))
{
/* If temp and result are not equal, replace them all the time */temp = result; Result = temp. replaceall (STRC, "");} system. out. println (result );}}

Difference between replace and replaceall

Some of them are easy to mix up, so I will detail them here.

The replace parameters are char and charsequence, which can be used to replace characters or replace strings (charsequence is the meaning of string sequences, that is, strings );

The replaceall parameter is RegEx, that is, the replacement based on the Rule expression. For example, you can replaceall ("\ D", "*") to replace all the numeric characters of a string with asterisks;

Similarities: replace all characters with specified characters or strings;

Difference: replaceall supports regular expressions, so the parameter will be parsed (both parameters are), such as replaceall ("\ D", "*"), while replace does not, replace ("\ D", "*") is a string that replaces "\ D" and is not parsed as a regular expression.

There is another difference: "\" is an escape character in Java, so two must be used to represent one. For example, system. Out. println ("\"); only one "\" is printed "\". However, "\" is also an escape character in a regular expression and must be represented by two characters. Therefore, \\\\ is converted to \\\, \\ by a regular expression, so replaceall is replaced with "\" "\\", replaceall ("\\\\", "\\\\\\\\") is required, while Replace ("\\", "\\\\").

If you only want to replace the string that appears for the first time, you can use replacefirst (). This method is also based on the Rule expression, but unlike replaceall (), it only replaces the string that appears for the first time.

String-02. Delete the substring from the string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.