23

In computer programming, when you append a "string" to another, you add it to the end of the former string.

E.g.

String string1 = "abcd";
String string2 = "efgh";

Appending the two strings would give

"abcdefgh"

But what if I wanted to add string2 before string1 to have the result:

"efghabcd"

What would be a good name for that that reverse operation? What is the opposite of "append"?

11
  • 13
    Given you have over 1500 rep on StackOverflow, I find it odd you're not familiar with prepend. Apart from anything else, I see it in function documentation all over the place. I think it's General Reference for any who might need to know it. Aug 23, 2012 at 19:43
  • 6
    You don’t add strings; you concatenate them.
    – tchrist
    Aug 23, 2012 at 23:40
  • 8
    Isn't the opposite of append (adding something to a string) trim (removing something from a string)?
    – Joel B
    Aug 24, 2012 at 0:18
  • 3
    @FumbleFingers You're assuming CodeBlue is a native English speaker. There's nothing in his/her profile to indicate if that's true either way. Aug 24, 2012 at 12:44
  • 4
    @ΜετάEd, except as ЯegDwight noted in a comment on his answer, there's a bit of "can't get there from here": you can't look up "append" in a thesaurus and get "prepend" out of it, and if you don't know that the word "prepend" exists, you can't look it up in the dictionary.
    – Marthaª
    Aug 24, 2012 at 14:07

4 Answers 4

63

Prepend:

(computing, linguistics, transitive) To attach (an expression, phrase, etc.) to another, as a prefix.

5
  • 2
    I'm always too slow on the draw for these general reference questions. I need to make a script that nicely formats the definition by just putting a dictionary link in my answer. No additional copy/paste required. :(
    – Zairja
    Aug 23, 2012 at 18:37
  • 1
    @Zairja I actually took my time to browse thesaurus.com first. All it had on offer was "disjoin, subtract, take away". Looks like they haven't caught up on the latest lingo.
    – RegDwigнt
    Aug 23, 2012 at 18:47
  • 5
    Prepend was the first thing I thought of. Where was I an hour ago? Aug 23, 2012 at 20:14
  • 3
    Funny, to me opposite of append is shorten or truncate or remove or chop or something. It’s like how the opposite of Perl’s pop is more generally held to be push than it is to be shift.
    – tchrist
    Aug 23, 2012 at 23:39
  • @tchrist but isn't that generally true for programmers :P? We push things onto the stack, and then pop them off of it.
    – ardent
    Aug 23, 2012 at 23:46
13

Prepend: To attach (an expression, phrase, etc.) to another, as a prefix

I would also consider using concatenate, since this is often used to describe joining two strings together (and many computer languages have some kind of native CONCATENATE function).

Although the order of appending is implied by the order the strings are listed in, one can make it explicit by writing "append X to Y", which would result in YX.

If you wanted to describe the "reverse operation", you could simply reverse the order of variables in your sentence: "I'd like to append string1 to string2" (resulting in efghabcd).

6
  • 1
    so: prepend and postpend are the two types of append?
    – GEdgar
    Aug 23, 2012 at 18:38
  • @GEdgar Postponing is not the same as appending. Append means to add on (like a suffix, or writing additional data to a file). Prepend means to add on at the beginning like a prefix. You could use concatenate, as well.
    – Zairja
    Aug 23, 2012 at 18:40
  • 1
    @GEdgar: There was a word prepend long ago, nothing to do with computer neologism prepend - causing some to extrapolate postpend (to match other computery words like pre-increment, post-increment). But append has always implied add at the end of. Aug 23, 2012 at 19:50
  • prepend: "... to add or append (a character, string, file, etc.) at the front of an existing string, file, etc." (OED)
    – GEdgar
    Aug 23, 2012 at 21:37
  • 1
    @starNamer- concatenate does not imply more than two strings. see here for example you can certainly concatenate multiple strings easily but each one adds to the result of the previous binary operation
    – Jim
    Aug 24, 2012 at 1:24
1

Both dot-net and Java StringBuilder libraries, and probably comparable things in other libraries, have an "insert" function that allows you to insert a new string at an arbitrary place in a target string. Location zero would be at the beginning, location 1 is after the first character, etc.

Thus, my practical answer to your question in a programming context is that the opposite of "append" is "insert(0)".

Note that insert is useful to insert at any known offset into a string. It is not so useful for inserting at the end of the string because we then need an extra step to find just where the end is. As this is an extremely common thing to want to do, it makes sense to have a separate append function.

In general English, we say "prepend", as others have noted. But I don't think most string-builder-type objects have a prepend function, as it would be rather redundant with insert.

6
  • 3
    Psh, kids today are so spoiled. cons is all you ever need for anything (and is not a bad answer here, methinks).
    – Rachel
    Aug 23, 2012 at 23:37
  • 1
    insert? Bah. Just splice. But the opposite of appending something to the end is removing something from the end. Also, many sequences are optimized for adding or removing at either end.
    – tchrist
    Aug 23, 2012 at 23:41
  • @Rachel - You've got to be cdr-ing.
    – user16269
    Aug 24, 2012 at 3:03
  • @tchrist Sure, many words could have different antonyms depending on the sense in which you mean "opposite". Like what is the opposite of "man"? You could say "woman", "boy", "animal", "machine", and probably many other words, in one sense or another.
    – Jay
    Aug 24, 2012 at 14:09
  • 1
    Or "mouse", as in, "What are you, a man or a mouse?" Or on Linux, "info", as in "man pages" versus "info pages". We could play this game all day!
    – Jay
    Aug 24, 2012 at 19:23
0

Prepend, insert, pre-attach. Some programming languages have additional functions that can add strings in various positions, including the start of another string. For example: slice, push, at.

Not the answer you're looking for? Browse other questions tagged or ask your own question.