# String subtraction

## Objective

Create a function to reverse string concatenation

## 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.

## Test Cases

### Valid Subtraction

'abcde','ab' -> 'cde'
'abcde','cde' -> 'ab'
'abab','ab' -> 'ab'
'abcab','ab' -> 'abc' or 'cab'
'ababcde','ab' -> 'abcde'
'acdbcd','cd' -> 'acdb'
'abcde','abcde' -> ''
'abcde','' -> 'abcde'
'','' -> ''


### Invalid Subtraction (returns original string)

'abcde','ae' -> 'abcde'
'abcde','aa' -> 'abcde'
'abcde','bcd' -> 'abcde'
'abcde','xab' -> 'abcde'
'abcde','yde' -> 'abcde'


### Invalid Input (don't need to be handled)

'','a' -> ''


This is , so the shortest code in bytes wins!

• 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? – Leaky Nun May 16 '17 at 16:01
• Damn you, 'abcde','bcd' -> 'abcde', for breaking my solution – John Dvorak May 16 '17 at 16:26
• Can we assume the strings will be regex-safe (alphanumeric + spaces)? – John Dvorak May 16 '17 at 16:27
• I'd suggest 'ababcde', 'ab''abcde' as a test case. Some naive algorithms fail on that one. – user62131 May 16 '17 at 16:48
• @Rod You might consider retitling the challenge "Reverse string concatenation"? – MD XF May 16 '17 at 19:52

# Pip, 19 bytes

Y'^.X:baRyNa?yb.'$x  Try it online! Kinda sad this isn't shorter. The main problem is that Pip's R operator replaces all matches, not just the first one; otherwise, aR"^|$"J Xbx would work for 12 bytes.

                     a, b are command-line args; x is empty string (implicit)
X:b              Convert b to a regex in-place
'^.                 Prepend ^
Y                    and yank that regex into the y variable
aR         x  In a, replace the following expression with the empty string:
yNa?         Is there a match of y in a? I.e., does b match at the beginning of a?
y        If so, replace y (b at the beginning of a)
b.'$If not, replace b with$ appended (b at the end of a)


# Kotlin, 6242 56 bytes

+12 bytes I think that type definitions are mandatory in the Kotlin lambda with 2 arguments

{a:String,b:String->Regex("^$b|$b$").replaceFirst(a,"")}  Test script: var f = {a:String,b:String->Regex("^$b|$b$").replaceFirst(a,"")}

fun main(args: Array<String>) {
println(f("abcde","ab"))
println(f("abcab","ab"))
println(f("abcde","bcd"))
}


Output:

cde
cab
abcde


## Kotlin (without Regex), 62 76 bytes

{a:String,b:String->var c=a.removePrefix(b)
if(a==c){c=a.removeSuffix(b)}
c}