33
\$\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
    \$\begingroup\$ No one else answer, C# is winning... too late :( \$\endgroup\$ – TheLethalCoder May 19 '17 at 13:55
  • \$\begingroup\$ Can we assume that the char at that idx appears only once? \$\endgroup\$ – programmer5000 May 19 '17 at 13:59
  • 1
    \$\begingroup\$ @programmer5000 Last test case 3, 314151 -> 31451. I'd assume not. \$\endgroup\$ – TheLethalCoder May 19 '17 at 14:01
  • \$\begingroup\$ @programmer5000 No. See the last test case. \$\endgroup\$ – ETHproductions May 19 '17 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 '17 at 4:26

74 Answers 74

0
\$\begingroup\$

Braingolf, 23 bytes

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

Try it online!

Not at all golfy

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

Excel, 20 bytes

Different approach from previous Excel answer.

1-indexed n in A1. Text in B1.

=REPLACE(B1,A1,1,"")
\$\endgroup\$
0
\$\begingroup\$

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.

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

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.

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

><>, 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
\$\endgroup\$
0
\$\begingroup\$

Backhand, 12 bytes

{I>{{[ ~:oi_

Try it online!

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

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

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                                              |
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

JavaScript 30 bytes

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

Try it online!

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

Tcl, 37 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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