Remove character at specified index

33

0

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

ETHproductions

Posted 2017-05-19T13:52:18.080

Reputation: 47 880

9No one else answer, C# is winning... too late :( – TheLethalCoder – 2017-05-19T13:55:11.740

Can we assume that the char at that idx appears only once? – programmer5000 – 2017-05-19T13:59:54.503

1@programmer5000 Last test case 3, 314151 -> 31451. I'd assume not. – TheLethalCoder – 2017-05-19T14:01:12.037

@programmer5000 No. See the last test case. – ETHproductions – 2017-05-19T14:01:33.900

Can we take n as a character code? Or must it be in decimal? (for languages like brainfuck, brain-flak, etc.) – James – 2017-05-19T16:46:46.797

@DJMcMayhem I think char-code input would be fine. – ETHproductions – 2017-05-19T17:09:08.813

2Maybe a leaderboard would be helpful, there are plenty of answers to search through already. – Mr. Xcoder – 2017-05-20T04:26:09.957

Are we allowed to modify string s in place, or do I have to actually return a new string? – 12Me21 – 2017-05-20T18:56:11.290

@12Me21 I believe there was a meta post about this and the general consensus was that functions can modify their arguments in place. So if e.g. var s="string"; f(s,2); print(s); prints the proper result, then yes. – ETHproductions – 2017-05-20T19:22:43.853

Answers

23

C#, 20 19 bytes

s=>n=>s.Remove(n,1)

TheLethalCoder

Posted 2017-05-19T13:52:18.080

Reputation: 6 930

13

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.

Martin Ender

Posted 2017-05-19T13:52:18.080

Reputation: 184 808

10

Japt, 2 bytes

jV

Try it online!

Tom

Posted 2017-05-19T13:52:18.080

Reputation: 3 078

1Bah, you beat me to it by a minute! – Shaggy – 2017-05-19T14:09:20.673

10

K (Kona), 1 byte

_

Gotta love builtins. 0-based indexing. Usage:

k)"abcdef" _ 3
"abcef"

Simon Major

Posted 2017-05-19T13:52:18.080

Reputation: 401

Giving a whole new meaning to 'using the right tool for the job'. – MD XF – 2017-05-20T20:45:36.357

1

Ha - you want to see right tool for the job? https://codegolf.stackexchange.com/a/121700/49493

– Simon Major – 2017-05-20T22:05:25.280

I have discovered a way to do this with an even shorter program. Unfortunately, there is not enough room in this comment box to explain ;-)

– Mawg says reinstate Monica – 2017-05-22T07:50:59.827

8

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.

Sgt. Doggo

Posted 2017-05-19T13:52:18.080

Reputation: 111

2Welcome to PPCG! – Martin Ender – 2017-05-19T14:09:54.013

1

Also you might be interested in the collection of tips for golfing in Haskell.

– Laikoni – 2017-05-19T14:36:21.023

7

Mathematica, 18 bytes

1-indexed

#2~StringDrop~{#}&

input

[1, "abcde"]

thanks Martin Ender

J42161217

Posted 2017-05-19T13:52:18.080

Reputation: 15 931

4In 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 :) – Greg Martin – 2017-05-19T18:06:28.773

6

V, 3 bytes

À|x

Try it online!

This uses 1-indexing.

James

Posted 2017-05-19T13:52:18.080

Reputation: 54 537

5

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.

Business Cat

Posted 2017-05-19T13:52:18.080

Reputation: 8 927

5

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.

Digital Trauma

Posted 2017-05-19T13:52:18.080

Reputation: 64 644

2In 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! – Neil – 2017-05-19T17:06:21.530

5I love it when C answers go "this has no official reason to work, but it does anyway, so... eh." – Quentin – 2017-05-20T17:25:17.857

Holy crap. This blows mine out of the water. Very impressive! – MD XF – 2017-05-20T20:46:04.433

1@Quentin That's one of the fun things about [tag:code-golf] - you are allowed - encouraged even - to write the most awful, unsafe code that would normally be a firing offence ;-) – Digital Trauma – 2017-05-22T17:58:37.007

I'd love to know the reason for the downvote... – Digital Trauma – 2017-05-23T17:54:29.517

4

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.

Luis Mendo

Posted 2017-05-19T13:52:18.080

Reputation: 87 464

I like the idea of generic command modifiers, they might be useful in other golfing languages. – ETHproductions – 2017-05-19T14:15:46.877

2@ETHproductions If you need a name, I call them meta-functions, as they modify functions – Luis Mendo – 2017-05-19T14:19:01.637

@LuisMendo I think the formal name would be operators, a la mathematical operators (aka higher-order functions). – Mego – 2017-05-21T03:25:16.147

4

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

jmriego

Posted 2017-05-19T13:52:18.080

Reputation: 381

Another fun solution that's almost identical is jD@"gox – James – 2017-05-19T19:08:57.990

Flagging -> Closing -> Duplicate of https://codegolf.stackexchange.com/a/121581/61563 :P kidding, but they are remarkably similar.

– MD XF – 2017-05-20T20:44:56.673

they are! Is there any prize for getting down to 7 characters first? :-P – jmriego – 2017-05-22T07:58:28.840

4

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.

Kevin Cruijssen

Posted 2017-05-19T13:52:18.080

Reputation: 67 575

Assuming it works, a "built in" for 46 bytes s->n->new StringBuilder(s).deleteCharAt(n)+""; though it is longer. – TheLethalCoder – 2017-05-19T15:03:03.420

@TheLethalCoder It indeed works. But it's indeed a bit longer. Oh, and always use StringBuffer instead of StringBuilder in codegolf. ;)

– Kevin Cruijssen – 2017-05-19T15:04:46.743

Ah nice trick on the buffer I used it in my answer :) – TheLethalCoder – 2017-05-19T15:30:49.953

4

Ruby, 16 bytes

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

Try it online!

Alex

Posted 2017-05-19T13:52:18.080

Reputation: 371

4

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.

H.PWiz

Posted 2017-05-19T13:52:18.080

Reputation: 10 962

4

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])

J.Doe

Posted 2017-05-19T13:52:18.080

Reputation: 2 379

3

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

programmer5000

Posted 2017-05-19T13:52:18.080

Reputation: 7 828

3

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!

Doorknob

Posted 2017-05-19T13:52:18.080

Reputation: 68 138

3

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

Riley

Posted 2017-05-19T13:52:18.080

Reputation: 11 345

3

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

Emigna

Posted 2017-05-19T13:52:18.080

Reputation: 50 798

3

Pyth, 3 bytes

.DE

Try it here.

Takes index first.

Erik the Outgolfer

Posted 2017-05-19T13:52:18.080

Reputation: 38 134

3

PHP, 42 Bytes

0 indexed

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

Try it online!

Jörg Hülsermann

Posted 2017-05-19T13:52:18.080

Reputation: 13 026

3

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.

TheLethalCoder

Posted 2017-05-19T13:52:18.080

Reputation: 6 930

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.
  • < – Olivier Grégoire – 2017-05-22T12:59:40.527

    @OlivierGrégoire Thanks :) I've never actually used Java before so all improvements are welcome – TheLethalCoder – 2017-05-22T13:02:34.187

    3

    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
    

    Jonathan Allan

    Posted 2017-05-19T13:52:18.080

    Reputation: 67 804

    2

    Python 3, 24 bytes

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

    Try it online!

    Leaky Nun

    Posted 2017-05-19T13:52:18.080

    Reputation: 45 011

    Dam you beat me to it! – Notts90 supports Monica – 2017-05-19T13:56:33.170

    1I think, that this is valid for python 2 too – Dead Possum – 2017-05-19T14:02:03.540

    2

    JavaScript (ES6), 39 34 33 bytes

    n=>s=>s.replace(/./g,c=>n--?c:"")
    
    • 5 6 bytes saved thanks to Arnauld.

    Shaggy

    Posted 2017-05-19T13:52:18.080

    Reputation: 24 623

    2

    Japt, 3 2 bytes

    jV
    

    Try it online!

    Luke

    Posted 2017-05-19T13:52:18.080

    Reputation: 4 675

    You don't need the 1 – Oliver – 2017-06-06T19:51:27.940

    Thanks a lot. Not sure how I missed that... – Luke – 2017-06-06T20:27:58.073

    2

    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.

    ovs

    Posted 2017-05-19T13:52:18.080

    Reputation: 21 408

    2

    brainfuck, 14 bytes

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

    Try it online!

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

    eush77

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 280

    Aw, you beat me to it :/ I had the exact same solution. +1 – daniero – 2017-05-19T21:35:28.103

    2

    R, 48 47 bytes

    (1 byte saved through use of el() thanks to Giuseppe)

    function(s,n)cat(el(strsplit(s,""))[-n],sep="")
    

    Split the string into its individual characters, remove the nth and then concatenate again.

    There may well be a better solution, strsplit() is quite unwieldy as it returns a list.

    user2390246

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 391

    won't work on TIO: pryr::f([function body]) saves a few bytes and using el(strsplit(s,"")) saves a byte but also doesn't work on TIO for some reason. – Giuseppe – 2017-05-23T18:25:39.933

    @Giuseppe Thanks! I would feel a bit dirty making use of pryr::f since surely it should be preceded by install.packages("pryr") but maybe that's me being too precious! – user2390246 – 2017-05-24T18:42:05.507

    function(s,n)intToUtf8(utf8ToInt(s)[-n]) for 40 bytes. – J.Doe – 2018-09-21T17:23:11.480

    @J.Doe good spot! That's a very different approach so you should post it as your own answer. – user2390246 – 2018-09-22T06:30:16.150

    Another sub-47 is function(s,n)sub(sub(0,n,"(.{0})."),"\\1",s) for 44. – J.Doe – 2018-10-05T15:46:49.127

    2

    PHP, 41 bytes, 35 bytes excluding ?php

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

    0-indexed

    TIO

    M.E

    Posted 2017-05-19T13:52:18.080

    Reputation: 91

    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. – None – 2017-05-23T07:18:44.767

    @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." http://php.net/manual/en/language.types.string.php

    – M.E – 2017-05-23T07:37:42.677

    2

    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

    Adám

    Posted 2017-05-19T13:52:18.080

    Reputation: 37 779

    2

    Cubically, 46 56 34 bytes

    R3D1R1$:7~?6{(~@7-0)6}~(~-0=7&6@7)
    

    (0-based index)

    Try it online! You must change &6 to ?6& on TIO due to a bug in the interpreter.

    How it works

    R3D1R1$:7~?6{(~@7-0)6}~(~-0=7&6@7)
    R3D1R1                             Set the top face to 1
          $                            Get next numeric input
           :7                          Set notepad to input
             ~                         Read (skip) next character (space)
              ?6{        }             If notepad is nonzero
                 (     )6              While notepad is nonzero
                  ~@7                  Print next inputted character
                     -0                Subtract 1 from notepad
                          ~            Read (skip) next character
                           (         ) Loop indefinitely
                            ~          Get next character
                             -0=7&6    Exit program if input is -1 (EOF)
                                   @7  Output the input
    

    TehPers

    Posted 2017-05-19T13:52:18.080

    Reputation: 899

    1

    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
    

    fireflame241

    Posted 2017-05-19T13:52:18.080

    Reputation: 7 021

    1

    Perl 5, 19 bytes

    18 bytes of code + -p flag

    substr($_,<>,1)=""
    

    Try it online!

    substr($_,<>,1) returns 1 character at index <> (the 2nd input) in the string $_ (the input). And ="" changes it to "". And $_ is implicitely printed at the end thanks to -p flag.

    Dada

    Posted 2017-05-19T13:52:18.080

    Reputation: 8 279

    1

    PowerShell, 25 Bytes

    3 Options, all the exact same bytecount.

    $a,$b=$args;$a|% *ve $b 1
    
    $args[1]|% *ve $args[0] 1
    
    param($a,$b)$a|% *ve $b 1
    

    calls .Remove on the string, using the number as an arg.

    colsw

    Posted 2017-05-19T13:52:18.080

    Reputation: 3 195

    1

    Go, 50 bytes

    1-indexed because we're all human, I think.

    func f(s string,n int)string{return s[:n-1]+s[n:]}
    

    Try it online!

    totallyhuman

    Posted 2017-05-19T13:52:18.080

    Reputation: 15 378

    Also note that the import statement is not being included because it is not being used in the function but rather for testing of it. Go requires an import to print stuff out. – totallyhuman – 2017-05-19T15:34:38.270

    1

    Lua, 37 bytes

    Uses 0 indexing

    s,i=...
    print(s:sub(1,i)..s:sub(i+2))
    

    Try it online!

    Felipe Nardi Batista

    Posted 2017-05-19T13:52:18.080

    Reputation: 2 345

    1

    C (gcc) - 45 44 bytes

    -1 Thanks to @Niel !

    f(s,n)char*s;{for(;*s;++s)n--&&putchar(*s);}
    

    alternative, 45 bytes :

    g(s,n)char*s;{s[n++]=0;printf("%s%s",s,s+n);}
    

    try it online

    Yk Cheese

    Posted 2017-05-19T13:52:18.080

    Reputation: 11

    1

    QBIC, 28 bytes

    B=@ `+B?_t_s;,:|+_sB,a+2,_lB
    

    This is an adaptation of my Substring Subtraction answer.

    Explanation:

    B=@ `+B     Prepend a space to the input string
    ?           PRINT
     _t           a trimmed version (drops the space again)
       _s         SUBSTRING
         ;,:|       Read B$ from cmd line, read n from cmd line
                    SUBSTRING with a stirng and a positive N takes chars 1-N from the left.
       +_sB       Plus a second substring
         ,a+2       Starting behind the removee-index
         ,_lB       taking all the remaining characters
    

    This could go all the way down to 22 bytes if I didn't need that hacky space prepended. Time to change Substring-behaviour for this use-case...

    ?_s;,:-1|+_sA,a+1,_lA
    

    steenbergh

    Posted 2017-05-19T13:52:18.080

    Reputation: 7 772

    1

    Javascript (ES6), 48 bytes

    x=>i=>x.substring(0,i-1)+x.substring(i,x.length)
    

    Or this (55 bytes)

    n=[],x=>i=>x.split``.forEach((_,i)=>i==x?0:n.push(_))
    

    user69538

    Posted 2017-05-19T13:52:18.080

    Reputation:

    Just wanted to say, community downvoted this post because it was flagged as "low quality" (I think) and then I edited it, causing it to downvote. Welcome to the site!

    – programmer5000 – 2017-05-19T16:03:16.693

    You can save some bytes in the second one by replacing ("") with \``, and (x,i) with x=>i in the first and second one. – programmer5000 – 2017-05-19T16:04:16.830

    1

    Retina, 29 bytes

    1`\d+
    $*	
    +`(	+)	(.)
    $2$1
    	.
    
    

    Try it online!

    The index and the string are on the same line separated by one tab. The index is zero-based.

    eush77

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 280

    1

    Chip, 115 bytes

    *Z~S
    ,x.z.
    `@z\{Aa
    ,x.|*
    `@z\{Bb
    ,x.|
    `@z\{Cc
    ,x.|*
    `@z\{Dd
    ,x.|
    `@z\{Ee
    ,x.|*
    `@z\{Ff
    ,x.|
    `@z\{Gg
    ,x.|*
    `@z\{H
     S
    

    The first byte of input is a 0-based index, the remainder is the string to process: \x02abcde.

    Try it online!
    In this TIO, a newline character is the index, which means an index of 0x0a = 10.

    Test cases!
    These use hex input + printf to make it clearer what's happening.

    How does it work?

    *Z~S

    This suppresses output on the first byte, because we don't want to print the index. That'd be silly.

    Aa, Bb, Cc, etc.

    These relay the input to the output for every byte, unless suppressed. Note that the h is missing, this is because we never need to output something with the high bit set.

    ,x.
    `@z\{D
    

    This is the real meat of it all. There are eight of these, one for each bit of the index (a one-byte index means that indexes are not able to be specified above 255). This reads in the index on the first cycle only with switches (\). The index bits have been flipped with an xor gate ({), which is almost negation. Then, for each character of the string, this value is incremented via some half-adders (@). When this value is incremented from 0xff to 0x00, it uses the carry from the high bit to suppress output (S) for that character, effectively removing it from the string.

    Phlarx

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 366

    1

    Bash, 83 24 22 bytes

    echo ${1::$2-1}${1:$2}
    

    24 bytes answer given by Digital Trauma

    -2 bytes, quotation marks not needed

    My first GodeGolf answer, any feedback is apreciated

    DrnglVrgs

    Posted 2017-05-19T13:52:18.080

    Reputation: 145

    1Welcome to PPCG! – Martin Ender – 2017-05-19T21:14:11.977

    How about echo "${1::$2-1}${1:$2}"? – Digital Trauma – 2017-05-20T06:01:28.983

    @DigitalTrauma welp, i tried that and thought it didnt work because i was testing with 0 indexed – DrnglVrgs – 2017-05-23T14:51:13.370

    1

    (s)ed - 6 bytes

    1-based indexing.

    s/.//n
    

    Explanation:

    s       # substitute command.
     /./    # match any single char.
       //   # substitution is empty.     
         n  # Only match the 'n'th occurrence in a line. 
    

    gdahlm

    Posted 2017-05-19T13:52:18.080

    Reputation: 119

    4Unfortunately, hardcoding part of the input into the program is not allowed. You'd have to get n from the user (STDIN, command-line arguments, etc.), which I'm not sure is possible in sed. There's always the option to wrap your solution in a Bash script though. – Dennis – 2017-05-20T14:23:15.843

    1If you do sed s/.//$1 and call it a BASH + GNU utilities answer, I think it will be a good 11-byte answer and I'll change my downvote to an upvote :) – Digital Trauma – 2017-05-20T16:39:35.233

    Yes the fun of being a newbie

    I'll re-read the linked thread and try to do it all within sed. I assumed that because all of the typed languages were sparse that it was preferred.

    I know I can resort to ssed if it is going to be "GNU utilities" without a shell, not that I am anti GNU but the 'n' flag is a 4.4BSD feature IIRC. – gdahlm – 2017-05-20T22:35:30.030

    1

    Excel, 29 bytes

    =LEFT(A1,A2)&MID(A1,A2+2,9^9)
    

    Assumes input in cells A1 (string) and A2 (integer, 0-indexing).

    Takes the left part of the string with LEFT and the right part - actually a substring starting on position A2+2 and 9^9 characters long (which is more then the max string length in one cell: 32,767 characters).

    pajonk

    Posted 2017-05-19T13:52:18.080

    Reputation: 2 480

    I think that you have miscounted your byte count. I see 29 bytes :) – Taylor Scott – 2017-05-22T14:40:46.740

    1@Taylor thanks, I must have had a trailing space when pasting into byte count tool. – pajonk – 2017-05-22T15:19:20.130

    1

    R, 45 bytes

    pryr::f(sub(paste0("(.{",n-1,"})."),"\\1",s))
    

    Sven Hohenstein

    Posted 2017-05-19T13:52:18.080

    Reputation: 2 464

    couldn't you just use n instead of n-1 and say it's 0-indexed?

    Also, just for fun you could use sprintf('(.{%d}).',n-1) instead of the paste0 expression and it would have the same length as your current answer. – Giuseppe – 2017-05-23T18:33:22.683

    1

    SmileBASIC 3, 23 bytes

    We have character indexing, why not use it? Set a character index to the empty string to delete it.

    Defines a command A which prints the output. Uses 0-indexing.

    DEF A S,N
    S[N]="
    ?S
    END
    

    snail_

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 982

    1

    C, 57 50 bytes

    #define f(s,n)for(i=n;s[i];i++)s[i]=s[i+1];puts(s)
    

    0-indexed, naturally. I call this macro with:

    int main(int argc, char **argv)
    {
        if (argc != 3)
            return;
        f(argv[1],atoi(argv[2]));
    }
    

    MD XF

    Posted 2017-05-19T13:52:18.080

    Reputation: 11 605

    Could you shorten i<strlen(s); to s[i]? (I see you have done this in your shortC answer) Clever algorithm, btw. – ETHproductions – 2017-05-20T19:23:57.080

    @ETHproductions Oops, meant to put that in here. Thanks – MD XF – 2017-05-20T20:33:25.177

    1

    Lua, 51 38 bytes

    Lowered to 38 bytes thanks to Jo King

    x,y=...print(y:sub(1,x-1)..y:sub(x+1))
    

    Try it online!

    Just like everything else in Lua this is 1-indexed.

    Explanation

    Lua uses a table called arg to store arguments and the function sub() returns a substring of the string passed.

    This is my first code golf entry, please be nice :)

    Marcio Medeiros

    Posted 2017-05-19T13:52:18.080

    Reputation: 51

    You wrote wlse -- did you mean wise? – Jonathan Frech – 2018-09-20T21:09:48.900

    Welcome to PPCG! You can use ... to get this down to 38 bytes!

    – Jo King – 2018-09-21T00:35:03.317

    1

    q, 6 1 byte

    _
    

    Test Cases

    q)"abcde"_0
    "bcde"
    q)"abcde"_1
    "acde"
    q)"a != b"_2
    "a = b"
    etc.
    

    Thaufeki

    Posted 2017-05-19T13:52:18.080

    Reputation: 421

    why not just _? :) it's ok to swap the arguments, many answers already do that – ngn – 2018-09-28T01:11:57.837

    I suppose you're right, it's built in functionality, thanks – Thaufeki – 2018-09-28T01:33:12.830

    1

    Rebol, Red - 12 bytes

     remove at s n
    

    HappySpoon

    Posted 2017-05-19T13:52:18.080

    Reputation: 11

    2Hello and welcome to PPCG. I am unfamiliar with Rebol -- does your code reflect a full program or any function-esk construct? Snippets do not count as valid submissions to standard code golf challenges. – Jonathan Frech – 2018-09-21T01:23:52.477

    This does not appear to be valid - we require solutions to be either full programs or functions, unless stated otherwise. We don't allow the assumption that input is already in predefined variables. In order for this to be a valid function submission, it would need to be func [s n] [remove at s n] (assuming I'm reading the documentation correctly). – Mego – 2018-09-21T02:22:42.823

    1

    Runic, 48 bytes

       /~~{ R:1(?\~@
    /3{\?)0:\S{-1/
    \s3s}}1-U}:irui<
    

    Try it online!

    Not all test cases work at the moment: a few (more) flaws discovered in the parser.

    • Numbers are read as numerical values and don't unconcatenate (I need to call .ToString() on the popped object, then split the result into a char array; this will match intended spec of "doing operations to the best of the execution context's ability with the given stack values)
    • Spaces (and other white space) are input value separators (I need to readd the control character demarcation; got lost in the previous attempts to fix the next issue). After fixing inputs such as a != b would need to be passed as a\ !=\ b
    • Input needs a trailing newline (have not discovered a fix to this yet)

    Other test cases work (and are considered 0-indexed).

    Explanation

    Flow

    Program starts in the lower right (red), reads a string, unconcatenates it, reverse it (so the first character is at the top of the stack; behavior is so that u followed by q results in concatenation in the desired order, and q works the way it does because >'a'bq, >`ab`q, and >"ab" should all have the same result). Then the program reads the integer value and assumes (per question spec) that it is in the range [0,L), values above this range will simply loop (effective %). This value is duplicated and stored at the bottom of the stack for later.

    The lower left portion (yellow) is the section responsible for shifting the string around on the stack N times, where N is the input value. The 3s3s portion rotates the top 2 items on the stack below the 3rd (which results in the front-most character in the string being rotated to the end without affecting the positions of the two counters with the next two } rotates). It exits this branch along the top (green), heading right and popping 2 values (the loop counter and then the character to discard) and rotating the saved input value back to the top.

    The top right (blue) then loops the remaining characters back into place in reverse, exiting the loop (purple) towards the @ which discards the remaining loop counter and prints the stack top to bottom.

    Picture has a mistake, along the bottom are some stack manipulators, }}}{ and in typing up this post I noticed that }{ cancels out and saves 3 bytes.

    Draco18s no longer trusts SE

    Posted 2017-05-19T13:52:18.080

    Reputation: 3 053

    0

    Braingolf, 38 37 bytes

    VVR<lMMv.M2+-[R<vv]R$_v.?1-[R<v]|R&@;
    

    Try it online!

    0-based index

    As always with Braingolf, the TIO link includes the entire Python3 parser. The code is the 2nd argument, the index is the 3rd argument, and the string is the 4th argument.

    Skidsdev

    Posted 2017-05-19T13:52:18.080

    Reputation: 9 656

    0

    Brain-Flak, 39 bytes

    {({}<({}<>)<>>[()])}{}{}<>{({}<>)<>}<>
    

    Try it online!

    This has a one byte penalty for the -c flag which enables ASCII input and output. This takes the first parameter as a character code, which OP has allowed.

    So the input is

    0x01abcde
    

    This uses zero indexing. Unfortunately, TIO doesn't allow null bytes in input, but this works locally so it's not an issue.

    James

    Posted 2017-05-19T13:52:18.080

    Reputation: 54 537

    0

    J, 8 bytes

    {.,1}.}.
    

    Uses zero-based indexing.

    Try it online!

    Explanation

    {.,1}.}.  Input: integer n (LHS), string s (RHS)
          }.  Drop the first n chars of s
       1}.    Drop one char from that
    {.        Take the first n chars of s
      ,       Join them
    

    miles

    Posted 2017-05-19T13:52:18.080

    Reputation: 15 654

    0

    Swift - 86 bytes (0-indexed)

    func l(s:String,n:Int){var a=s;a.remove(at:a.index(a.startIndex,offsetBy:n));print(a)}
    

    Function that can be used as l(s:"abcdefg",n:3)

    Check it out!


    Swift - 120 bytes

    Since I thought the above is quite boring, I came up with another solution:

    func l(s:String,n:Int){let b=s.startIndex;print(s[b...s.index(b,offsetBy:n-1)]+s[s.index(b,offsetBy:n+1)..<s.endIndex])}
    

    Works as the one below, it's just more interesting.

    Mr. Xcoder

    Posted 2017-05-19T13:52:18.080

    Reputation: 39 774

    0

    shortC, 36 bytes

    Df(s,n)Oi=n;s[i];i++)s[i]=s[i+1];Js)
    

    See my plain C answer for how to call the macro.

    MD XF

    Posted 2017-05-19T13:52:18.080

    Reputation: 11 605

    0

    REXX 30 Bytes

    parse arg p x
    say delstr(x,p,1)
    

    theblitz

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 201

    0

    Batch, 56 bytes

    @set/ps=
    @set/ai=%1+1
    @call echo %%s:~0,%1%%%%s:~%i%%%
    

    Takes n 0-indexed as a command-line parameter and reads s from STDIN.

    Neil

    Posted 2017-05-19T13:52:18.080

    Reputation: 95 035

    0

    ><>, 24 bytes

    i:0(?v$1-:}?!~{
    ;?(3l<o{
    

    Try it online, or at the fish playground

    Takes the string as input from STDIN, and assumes the number for the index is already on the stack; outputs to STDOUT. It's 1-indexed.

    The fish reads the string one character at a time, except it forgets the nth one after it reads it. It then prints the stack (in reverse) until there are only two (junk) things left.

    Example: 4 and Boarfish becomes Boafish.

    Not a tree

    Posted 2017-05-19T13:52:18.080

    Reputation: 3 106

    0

    Braingolf, 23 bytes

    VRMv.1-[R<v]R$_[R>v]R&@
    

    Try it online!

    Not at all golfy

    Skidsdev

    Posted 2017-05-19T13:52:18.080

    Reputation: 9 656

    0

    Excel, 20 bytes

    Different approach from previous Excel answer.

    1-indexed n in A1. Text in B1.

    =REPLACE(B1,A1,1,"")
    

    Wernisch

    Posted 2017-05-19T13:52:18.080

    Reputation: 2 534

    0

    QBasic, 49 bytes

    Uses 1-indexing.

    INPUT n
    LINE INPUT s$
    ?LEFT$(s$,n-1)+MID$(s$,n+1)
    

    Reads a number and a string, and then prints the leftmost n-1 characters of the string plus everything from index n+1 to the end of the string.

    DLosc

    Posted 2017-05-19T13:52:18.080

    Reputation: 21 213

    0

    T-SQL, 28 bytes

    SELECT STUFF(s,n,1,'')FROM t
    

    Input is taken via a pre-existing table t with integer n and string s, per our IO standards.

    I'm assuming n is 1-based, so I can pass it straight into the SQL Stuff function. STUFF inserts or replaces the characters at the given location, and in my case I'm replacing a single character with a blank.

    BradC

    Posted 2017-05-19T13:52:18.080

    Reputation: 6 099

    0

    ><>, 9 bytes

    i$:&?o&1-
    

    Try it online!

    Takes in the number via the -v flag and the string through STDIN. This is 0 indexed and ends with an error. You can add 5 bytes to avoid the error.

    Explanation:

    i           Get inputted character
     $          Swap and bring counter to the top
      :&        Store a copy in the register
        ?o      If the counter is not zero, print the character
          &1-   Restore and decrement the counter
    

    Jo King

    Posted 2017-05-19T13:52:18.080

    Reputation: 38 234

    0

    Backhand, 12 bytes

    {I>{{[ ~:oi_
    

    Try it online!

    Gets the inputted number followed by the string. 1 indexed.

    Jo King

    Posted 2017-05-19T13:52:18.080

    Reputation: 38 234

    0

    05AB1E, 4 bytes

    IõIǝ
    

    Try it online or verify all test cases.

    or alternatively

    õŠŠǝ
    

    Try it online or verify all test cases.

    Explanation:

    I       # Take the first string input
     õ      # Push an empty string
      I     # Take the second index-integer input
       ǝ    # Replace the character at the given index with the empty string
    
    õ       # Push an empty string
     ŠŠ     # Triple-swap twice; a,b,c → c,a,b → b,c,a (implicitly using the two inputs)
            #  (`c` is the empty string here, `a` and `b` the input)
       ǝ    # Replace the character at the given index with the empty string
    

    Kevin Cruijssen

    Posted 2017-05-19T13:52:18.080

    Reputation: 67 575

    0

    Forth (gforth), 45 bytes

    : f tuck - 1- -rot 2dup type + 1+ swap type ;
    

    Try it online!

    0-indexed, n should be on top of the stack

    Explanation

    Prints the string up to the given character, then prints the remainder of the string after the given character

    Code Explanation

    \ since a lot of stack manipulation is happening, I will be including a listing of the
    \ stack after each step. s = string address, g = length of string, n = char to remove
    : f          \ starts a new word definition                                     | s g n
      tuck -     \ store a copy of n, then get the length of the end of the string  | s n g-n
      1-         \ subtract 1 from the length of the end ( to not output junk)      | s n g-n-1
      -rot       \ move the end-string length to the back of the stack              | g-n-1 s n
      2dup type  \ duplicate the address and length of the first output, then print | g-n-1 s n
      + 1+       \ get the starting address for the second output                   | g-n-1 s+n+1
      swap type  \ swap to get in proper order, then print                          | 
    ;            \ end word definition                                              |
    

    reffu

    Posted 2017-05-19T13:52:18.080

    Reputation: 1 361

    0

    Attache, 13 bytes

    {_[0:#_^^_2]}
    

    Try it online!

    Explanation

    {_[0:#_^^_2]}
    {           }    anonymous lambda (_ = 1st argument, _2 = 2nd argument)
       0:#_          range from 0 to the length of _
           ^^_2      ...without the 2nd argument
     _[        ]     obtain characters from _ using that range
    

    Conor O'Brien

    Posted 2017-05-19T13:52:18.080

    Reputation: 36 228

    0

    Stacked, 17 bytes

    {%y:0\#'..x-keep}
    

    Try it online!

    Explanation

    {%y:0\#'..x-keep}    anonymous lambda, takes input on the stack
    {%              }    parameters: x, y
      y:                 push y twice             [y, y]
        0\               push 0 beneath           [y, 0, y]
          #'             push size of TOS         [y, 0, size(y)]
            ..           range                    [y, range(0, size(y))]
              x-         subtract x from this     [y, range(0, size(y)) - x]
                         (thus x-x = 0)
                keep     keep respective chars    [the answer]
                         this removes the character at the `x`th index
    

    Conor O'Brien

    Posted 2017-05-19T13:52:18.080

    Reputation: 36 228

    0

    MBASIC, 46 45 bytes

    1 INPUT N,S$:PRINT LEFT$(S$,N-1);MID$(S$,N+1)
    

    Just slamming substrings together, 1-indexed.

    Output:

    ? 4,314151
    31451
    
    ? 5,1234.5
    12345
    
    ? 4,+-*/
    +-*
    
    ? 3,a != b
    a = b
    
    ? 2,abcde
    acde
    
    ? 1,abcde
    bcde
    

    wooshinyobject

    Posted 2017-05-19T13:52:18.080

    Reputation: 171

    0

    PHP, 43 Bytes

    Try it online

    run as pipe.

    Code

    <?=strtr(($s=$argv)[0],[$s[0][$s[1]]=>""]);
    

    Explanation

    <?=strtr(($s=$argv)[0],        # Set a shorter name for the $argv arguments var
              [$s[0][$s[1]]=>""]); # Replace for "" in the position
    

    Francisco Hahn

    Posted 2017-05-19T13:52:18.080

    Reputation: 591

    0

    JavaScript 30 bytes

    (n,[...s])=>(s[n]='',s.join``)
    

    Try it online!

    guest271314

    Posted 2017-05-19T13:52:18.080

    Reputation: 1

    0

    Tcl, 37 bytes

    proc R n\ s {string repl $s $n $n ""}
    

    Try it online!

    sergiol

    Posted 2017-05-19T13:52:18.080

    Reputation: 3 055