37
\$\begingroup\$

(heavily inspired by Element of string at specified index)

Given a string s and an integer n representing an index in s, output s with the character at the n-th position removed.

0-indexing and 1-indexing are allowed.

  • For 0-indexing, n will be non-negative and less than the length of s.
  • For 1-indexing, n will be positive and less than or equal to the length of s.

s will consist of printable ASCII characters only (\x20-\x7E, or   through ~).

Any reasonable input/output is permitted. Standard loopholes apply.

Testcases (0-indexed):

n s        output
0 "abcde"  "bcde"
1 "abcde"  "acde"
2 "a != b" "a = b"
3 "+-*/"   "+-*"
4 "1234.5" "12345"
3 "314151" "31451"

Testcases (1-indexed):

n s        output
1 "abcde"  "bcde"
2 "abcde"  "acde"
3 "a != b" "a = b"
4 "+-*/"   "+-*"
5 "1234.5" "12345"
4 "314151" "31451"

This is , so shortest answer in bytes wins.

\$\endgroup\$
9
  • 9
    \$\begingroup\$ No one else answer, C# is winning... too late :( \$\endgroup\$ May 19, 2017 at 13:55
  • \$\begingroup\$ Can we assume that the char at that idx appears only once? \$\endgroup\$
    – user58826
    May 19, 2017 at 13:59
  • 1
    \$\begingroup\$ @programmer5000 Last test case 3, 314151 -> 31451. I'd assume not. \$\endgroup\$ May 19, 2017 at 14:01
  • \$\begingroup\$ @programmer5000 No. See the last test case. \$\endgroup\$ May 19, 2017 at 14:01
  • 2
    \$\begingroup\$ Maybe a leaderboard would be helpful, there are plenty of answers to search through already. \$\endgroup\$
    – Mr. Xcoder
    May 20, 2017 at 4:26

78 Answers 78

22
\$\begingroup\$

C#, 20 19 bytes

s=>n=>s.Remove(n,1)
\$\endgroup\$
0
14
\$\begingroup\$

Alice, 13 12 bytes

Thanks to Leo for saving 1 byte.

/oI\!e]&
@ q

Try it online!

First line of the input is the string, second line is the 0-based index.

Explanation

/    Reflect to SE. Switch to Ordinal. While in Ordinal mode, the IP bounces
     diagonally up and down through the code.
I    Read the first line of input (the string).
!    Store the string on the tape, which writes the characters' code points to 
     consecutive cells (the tape is initialised to all -1s).
]    Move the tape head right. This moves it by an entire string, i.e. to the
     cell after the -1 that terminates the current string.
     The IP bounces off the bottom right corner and turns around.
]    Move the tape head right by another cell.
!    Store an implicit empty string on the tape, does nothing. It's actually
     important that we moved the tape head before this, because otherwise it
     would override the first input code point with a -1.
I    Read the second line of input (the index) as a string.
/    Reflect to W. Switch to Cardinal.
     The IP wraps around to the last column.
&]   Implicitly convert the first input to the integer value it contains
     (the index) and move the tape head that many cells to the right, i.e.
     onto the character we want to delete. Note that Ordinal and Cardinal mode
     have two independent tape heads on the same tape, so the Cardinal tape
     head is still on the first cell of the string input before we do this.
e!   Store a -1 in the cell we want to delete.
\    Reflect to SW. Switch to Ordinal.
q    Push the entire tape contents as a single string. This basically takes
     all cells which hold valid code points from left to right on the tape 
     and concatenates the corresponding characters into a single string. Since
     we wrote a -1 (which is not a valid code point) over the target character,
     this will simply push the entire input string without that character.
o    Output the result.
@    Terminate the program.
\$\endgroup\$
10
\$\begingroup\$

Japt, 2 bytes

jV

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Bah, you beat me to it by a minute! \$\endgroup\$
    – Shaggy
    May 19, 2017 at 14:09
10
\$\begingroup\$

K (Kona), 1 byte

_

Gotta love builtins. 0-based indexing. Usage:

k)"abcdef" _ 3
"abcef"
\$\endgroup\$
3
8
\$\begingroup\$

Haskell, 28 24 Bytes

-4 byte thanks to Laikoni, this version is 1-indexed.

s#n=take(n-1)s++drop n s

Old answer:

f(s:t)0=t;f(s:t)n=s:f t(n-1)

A simple recursive function that takes the value, it's 0-indexed.

My first time code-golfing so maybe it's not the optimal solution. Oh well.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ May 19, 2017 at 14:09
  • 1
    \$\begingroup\$ Also you might be interested in the collection of tips for golfing in Haskell. \$\endgroup\$
    – Laikoni
    May 19, 2017 at 14:36
6
\$\begingroup\$

CJam, 4 bytes

q~Lt

Try it online!

Explanation

q~    e# Read and eval input (push the string and number to the stack).
  Lt  e# Set the nth element of the string to the empty string.
\$\endgroup\$
6
\$\begingroup\$

V, 3 bytes

À|x

Try it online!

This uses 1-indexing.

\$\endgroup\$
6
\$\begingroup\$

Mathematica, 18 bytes

1-indexed

#2~StringDrop~{#}&

input

[1, "abcde"]

thanks Martin Ender

\$\endgroup\$
1
  • 4
    \$\begingroup\$ In my opinion, "Any reasonable input/output is permitted" allows for the input to be taken like ["abcde", {1}], in which case StringDrop alone does the trick. What do you think? (You might want to explicitly mention that it's 1-indexed as well.) I'm always happy to see people posting Mathematica answers :) \$\endgroup\$ May 19, 2017 at 18:06
5
\$\begingroup\$

MATL, 3 bytes

&)&

Uses 1-based indexing.

Try it online! Or verify all test cases.

Explanation

&    % Specify secondary default number of inputs/outputs for next function
)    % Implicitly input string and number. Index: with & it pushes the char
     % defined by the index and the rest of the string
&    % Specify secondary default number of inputs/outputs for next function
     % Implicitly display (XD): with & it only displays the top of the stack

In the modified version with all the test cases, the code is within an infinite loop `...T until no input is found. At the end of each iteration the display function (XD) is explicitly called, and the stack is cleared (x) to ready it for the next iteration.

\$\endgroup\$
3
  • \$\begingroup\$ I like the idea of generic command modifiers, they might be useful in other golfing languages. \$\endgroup\$ May 19, 2017 at 14:15
  • 2
    \$\begingroup\$ @ETHproductions If you need a name, I call them meta-functions, as they modify functions \$\endgroup\$
    – Luis Mendo
    May 19, 2017 at 14:19
  • \$\begingroup\$ @LuisMendo I think the formal name would be operators, a la mathematical operators (aka higher-order functions). \$\endgroup\$
    – user45941
    May 21, 2017 at 3:25
4
\$\begingroup\$

Vim, 7 bytes

jDk@"|x

How it works:

It expects two lines; one with the string and one with the number.

  1. Go to line two, copy the number into register
  2. Go to first line and then go to column in the register with @"|
  3. Delete the character under the cursor
\$\endgroup\$
3
  • \$\begingroup\$ Another fun solution that's almost identical is jD@"gox \$\endgroup\$
    – DJMcMayhem
    May 19, 2017 at 19:08
  • \$\begingroup\$ Flagging -> Closing -> Duplicate of codegolf.stackexchange.com/a/121581/61563 :P kidding, but they are remarkably similar. \$\endgroup\$
    – MD XF
    May 20, 2017 at 20:44
  • \$\begingroup\$ they are! Is there any prize for getting down to 7 characters first? :-P \$\endgroup\$
    – jmriego
    May 22, 2017 at 7:58
4
\$\begingroup\$

Java 8, 39 bytes

s->n->s.substring(0,n)+s.substring(n+1)

Try it here.

Java 7, 67 bytes

String c(int n,String s){return s.substring(0,n)+s.substring(n+1);}

Try it here.

\$\endgroup\$
3
  • \$\begingroup\$ Assuming it works, a "built in" for 46 bytes s->n->new StringBuilder(s).deleteCharAt(n)+""; though it is longer. \$\endgroup\$ May 19, 2017 at 15:03
  • 1
    \$\begingroup\$ @TheLethalCoder It indeed works. But it's indeed a bit longer. Oh, and always use StringBuffer instead of StringBuilder in codegolf. ;) \$\endgroup\$ May 19, 2017 at 15:04
  • \$\begingroup\$ Ah nice trick on the buffer I used it in my answer :) \$\endgroup\$ May 19, 2017 at 15:30
4
\$\begingroup\$

Ruby, 16 bytes

->n,s{s[n]='';s}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

GCC c function, 25

1-based indexing.

f(n,s){strcpy(s-1,s+=n);}

Plenty of undefined behavior here so watch out for stray velociraptors:

  • The strcpy() man page says If copying takes place between objects that overlap, the behavior is undefined. Here there clearly is overlap of the src and dest strings, but it seems to work, so either glibc is more careful or I got lucky.
  • The answer is reliant on the fact that the s+=n happens before the s-1. The standard gives no such guarantees, and in fact calls this out as undefined behaviour. Again, it seems to work as required with the gcc compiler on x86_64 Linux.

Try it online.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ In a stack-based ABI, such as x86, strcpy's arguments need to be pushed in right-to-left order, which would explain the behaviour, but you said you were using x86_64 which uses registers... maybe the compiler decided to golf the generated code and decided that computing s+=n first was golfier! \$\endgroup\$
    – Neil
    May 19, 2017 at 17:06
  • 5
    \$\begingroup\$ I love it when C answers go "this has no official reason to work, but it does anyway, so... eh." \$\endgroup\$
    – Quentin
    May 20, 2017 at 17:25
  • \$\begingroup\$ Holy crap. This blows mine out of the water. Very impressive! \$\endgroup\$
    – MD XF
    May 20, 2017 at 20:46
  • 1
    \$\begingroup\$ @Quentin That's one of the fun things about code-golf - you are allowed - encouraged even - to write the most awful, unsafe code that would normally be a firing offence ;-) \$\endgroup\$ May 22, 2017 at 17:58
  • \$\begingroup\$ I'd love to know the reason for the downvote... \$\endgroup\$ May 23, 2017 at 17:54
4
\$\begingroup\$

Jelly, 3 bytes

Ṭœp

A full program taking the (1-based) index and the string (in that order) and printing the result.

As a dyadic function it returns a list of the two parts.

In fact the index may be a list of n indices, in which case it returns a list of the n-1 parts.

Try it online!, or see a test suite.

How?

Ṭœp - Main link: number i, string s                   e.g. "fish 'n chips", 6
Ṭ   - untruth - get a list with 1s at the indexes of i      000001 <-- i.e. [0,0,0,0,0,1]
 œp - partition s at truthy indexes without borders       ["fish ","n chips"]
    - implicit print                                        fish n chips

As an example of using multiple indexes:

      "fish and chips", [6,8]
Ṭ      00000101 <- i.e. [0,0,0,0,0,1,0,1]
 œp  ["fish ","n"," chips"] 
       fish n chips
\$\endgroup\$
4
\$\begingroup\$

Haskell, 15 bytes

This requires the recently released GHC 8.4.1 (or higher). Now <>, as a function on Semigroups, is in Prelude. It is particularly useful on the function Semigroup

take<>drop.succ

Try it online!
Since tio is using an older bersion of GHC, I've imported <> in the header.

\$\endgroup\$
0
4
\$\begingroup\$

R, 40 bytes

Just goes to show the variety of ways, none of which particularly compact, you can fiddle with strings in R.

function(s,n)intToUtf8(utf8ToInt(s)[-n])
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 6 bytes

vNÊiy?

Try it online!

Explanation

v       # for each element, index (y,N) in input1
 NÊi    # if N is not equal to input2
    y?  # print y
\$\endgroup\$
0
3
\$\begingroup\$

Pyth, 3 bytes

.DE

Try it here.

Takes index first.

\$\endgroup\$
3
\$\begingroup\$

JS (ES6), 41 32 31 bytes

y=>i=>y.slice(0,i++)+y.slice(i)

Based on this. Takes input through currying, first is string, second is index.

-9 thanks to @JohanKarlsson

-1 thanks to @ETHproductions

\$\endgroup\$
0
3
\$\begingroup\$

vim, 10 7

DgJ@"|x

Takes 1-indexed input in the following format:

2
abcde
D      delete the number on the first line into register "
gJ     remove the newline while preserving whitespace on line 2
@"     run the " register as a macro - input is used as a count for...
|      the "go to nth column" command
x      delete the character at the cursor

Thanks to @DJMcMayhem for 3 bytes!

\$\endgroup\$
0
3
\$\begingroup\$

brainfuck, 14 bytes

,[>,.<-],,[.,]

Try it online!

Reads zero-based one-byte index immediately followed by the string.

\$\endgroup\$
1
  • \$\begingroup\$ Aw, you beat me to it :/ I had the exact same solution. +1 \$\endgroup\$
    – daniero
    May 19, 2017 at 21:35
3
\$\begingroup\$

Java 8, 45 41 bytes

s->n->new StringBuffer(s).deleteCharAt(n)

Saved 4 bytes thanks to @OlivierGrégoire

My first code golf answer in something other than C#, even if it isn't the shortest for Java yet.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 1. You don't need the final ; in lambda (-1 bytes). 2. In my eyes, you don't need to return a String. I think that returning the StringBuffer without the +""would be perfectly valid (-3 bytes). Example? BigInteger is a representation of an unbounded int, in this case StringBuffer/StringBuilder are representations of mutable Strings. \$\endgroup\$ May 22, 2017 at 12:59
  • \$\begingroup\$ @OlivierGrégoire Thanks :) I've never actually used Java before so all improvements are welcome \$\endgroup\$ May 22, 2017 at 13:02
3
\$\begingroup\$

APL (Dyalog), 5 bytes

Zero-indexed. Takes index n as left argument and string s as right argument.

↑,1↓↓

Try it online!

n characters from s

, followed by

1↓ one character dropped from

n characters dropped from s

\$\endgroup\$
2
\$\begingroup\$

Python 3, 24 bytes

lambda n,a:a[:n]+a[n+1:]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Dam you beat me to it! \$\endgroup\$
    – Notts90
    May 19, 2017 at 13:56
  • 1
    \$\begingroup\$ I think, that this is valid for python 2 too \$\endgroup\$ May 19, 2017 at 14:02
2
\$\begingroup\$

Jelly, 4 bytes

Jḟị³

Try it online!

Explanation

Jḟị³
J    - all indices of the input string
 ḟ   - except for the input index
  ị  - return the elements at those indices
   ³ - of the input string
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 5 bytes

ā²ÊÏJ

Try it online!

ā     # push range(1, len(input string) + 1)
 ²Ê   # Check each for != to input index
   Ï  # Keep characters from input where this array is 1
    J # Join
\$\endgroup\$
2
\$\begingroup\$

PHP, 42 Bytes

0 indexed

<?=substr_replace($argv[1],"",$argv[2],1);

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Befunge-98, 35 27 25 bytes

-4 bytes thanks to @eush77

&#;1-:!#v_~,;
_@#:~;#~<;,

Try it online!

1-indexed, note that the input has a trailing null-byte.

\$\endgroup\$
0
2
\$\begingroup\$

PHP, 41 bytes, 35 bytes excluding ?php

<?php $argv[1][$argv[2]]='';echo$argv[1];

0-indexed

TIO

\$\endgroup\$
2
  • \$\begingroup\$ I'm actually really surprised this works; is the [$argv[2]] index implicitly creating a range? Also, IIRC you can leave the <?php off, because the PHP interpreter has a mode which doesn't need it, and because we don't normally penalise for that sort of indication in a file of what the language is. \$\endgroup\$
    – user62131
    May 23, 2017 at 7:18
  • \$\begingroup\$ @ais523 Basically yes. From docs: "Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose." php.net/manual/en/language.types.string.php \$\endgroup\$
    – M.E
    May 23, 2017 at 7:37
2
\$\begingroup\$

Japt, 3 2 bytes

jV

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You don't need the 1 \$\endgroup\$
    – Oliver
    Jun 6, 2017 at 19:51
  • \$\begingroup\$ Thanks a lot. Not sure how I missed that... \$\endgroup\$
    – Luke
    Jun 6, 2017 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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