Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Remove character at specified index

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

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ Assuming it works, a "built in" for 46 bytes s->n->new StringBuilder(s).deleteCharAt(n)+""; though it is longer. \$\endgroup\$ Commented 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\$ Commented May 19, 2017 at 15:04
  • \$\begingroup\$ Ah nice trick on the buffer I used it in my answer :) \$\endgroup\$ Commented May 19, 2017 at 15:30

AltStyle によって変換されたページ (->オリジナル) /