Objective

Input

Two strings (alphanumeric + spaces), where one should be subtracted for the other.

You can assume that the string to be subtracted will never be larger than the other one.

Output

The result from the subtraction

Subtraction

You should remove one string from the start or the end of another string.
If the string is present in the start and in the end, you can only remove one, which one will be removed is up to you.
If the string isn't in the start or in the end, or isn't an exact match, it is an invalid subtraction and you should output the original string.

\$\begingroup\$Why is the result of the first case not cde? What do you mean by valid? Do we need to judge the validity of the input, or do you mean that we will not receive invalid inputs?\$\endgroup\$
– Leaky NunMay 16 '17 at 16:01

\$\begingroup\$Why did you leave in the unused hashmap in your online example?\$\endgroup\$
– MichaelMay 17 '17 at 12:52

\$\begingroup\$You can change the First to All for -2 bytes. Because of the ^ and $ it's always either at the end or start of the String, so even with replaceAll it only replaces it once. Try it here. PS: I added the previous byte-counts striked-through to your answer, which is what usually is done after code-golf edits here on PPCG.\$\endgroup\$
– Kevin CruijssenMay 18 '17 at 9:00

\$\begingroup\$@KevinCruijssen I knew about the strike-throughs, guess I just forgot this time. However, if i use All instead of First, this becomes true: "abab" + "ab" -> ""\$\endgroup\$
– OkxMay 18 '17 at 9:43

Takes the string to subtract from from standard input, and the string to subtract as a command line argument.

Explanation

~cpĊh.∧Ċtw|w
~c Split {the input} into pieces
p and (possibly) rearrange those pieces
Ċ such that there are two pieces
h and the first
. matches the command line argument
∧ then
w print
t the last
Ċ piece.
| If all else fails,
w print {the input}.

\$\begingroup\$I have a question, why didn't you use Str1 as a variable?\$\endgroup\$
– ZacharýAug 9 '17 at 20:57

\$\begingroup\$@Zacharý I think I had something stored in it at the time. I don't really remember.\$\endgroup\$
– pizzapants184Aug 9 '17 at 21:02

\$\begingroup\$What does Ans even refer to on the fourth line?\$\endgroup\$
– ZacharýAug 9 '17 at 21:30

\$\begingroup\$@Zacharý Ans refers to the last evaluated value, so in this case it refers to the value returned by inString(, which is the index of the substring Str2 in the string Str0 or 0 if the substring does not appear. An if statement does not modify the value of Ans, so on the fourth line the index is still in Ans.\$\endgroup\$
– pizzapants184Aug 9 '17 at 21:34

\$\begingroup\$Nice solution! You can save a byte by using # instead of #1 — they mean exactly the same. Also, instead of using StringJoin@t, you can cheat by joining an empty string to it with ""<>t, which automatically joins everything in t together too. Have you seen the Mathematica golfing tips page?\$\endgroup\$
– Not a treeMay 17 '17 at 0:50

\$\begingroup\$There are a few more things you can do to save bytes (I don't think you don't need to define t={}; at the start, for instance), but it might be easier to use a different approach entirely — have you tried using the StringReplace function?\$\endgroup\$
– Not a treeMay 17 '17 at 1:13

\$\begingroup\$You are allowed to take a String array as input, so you don't really need c=Characters;a=c@#;b=c@#2;\$\endgroup\$
– JungHwan MinMay 17 '17 at 5:55

\$\begingroup\$Nice use of case, but longer than necessary. The 2nd and 3rd pattern could be merged into a single one: *)c=${1#$2};;. Then with only 2 branches would be shorter to echo each directly instead of using variable $c: case $1 in *$2)echo ${1%$2};;*)echo ${1#$2};;esac. Or you could keep using it, but without case: c=${1%$2};[[ $c = $1 ]]&&c=${1#$2};echo $c.\$\endgroup\$
– manatworkMay 18 '17 at 7:47

{
0::⍺{ ⍝ if an error happens, apply the following function on the arguments
0::⍺ ⍝ if an error happens, return the left argument unmodified
,∘⍵⍣¯1⊢⍺ ⍝ inverse-append right argument on left argument
}⍵
⍵,⍣¯1⊢⍺ ⍝ inverse-prepend the right argument on the left argument
}

Tcl is straightforward. proc s {a b} defines a function named s which takes parameters a and b. regsub substitutes {}, which is an empty string, for the value of b when it's at the start or end of a. The return is implicit.

One of the less readable programs I've written. Takes two inputs (despite how the function looks), a char** pointing to the string to deconcatenate and a char* which is the string to remove. The input pointer is edited in place and becomes the output (who cases about memory leaks anyway).

R, 2042 41 bytes

pryr::f(sub(sprintf('^%s|%s$',b,b),'',a))

-1 byte thanks to MickyT!

Returns an anonymous function (which has arguments in the order b,a). Computes the string difference a-b. sub is a simple substitution that swaps the first occurrence of the pattern with, in this case, the empty string ''. Constructs the regex with sprintf to match only at the beginning and end of string. Requires the pryr package to be installed.

On the TIO link, uses the more verbose function(a,b) definition for the function for four more bytes.

\$\begingroup\$"sub is a simple substitution that simply swaps the first occurrence of b in a": Will this swap if the second string is in the middle of the first string?\$\endgroup\$
– TheLethalCoderMay 16 '17 at 16:18

B=@ `+B┘x=instr(;,;)~x|?_t_sB,x-1|+_sB,x+_lC|,_lB|||\?B
B=@ `+B Prepend a string to B$. Thisis a hack to avoid errors with
removing substrings stating at index 1
┘ Line-break in QBasic output
(;,;) Read the string (B$) and the to-be-removed substring (C$)
x=instr And make x to be the starting index of the first C$ in B$
~x| IF X <> 0 (ie C$ is present in B$)
? PRINT
_t trimmed version (drops the prepended space)
_sB,x-1|+ of a substring from 1 to x (the start of C$) -1
_sB,x+_lC|,_lB and the rest of the string, starting after C$
_l takes the length of a string
||| End TRIM, end Substring, end Length
\?B When missing C$, just print B$

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.