# Element of string at specified index

Given a string s and a non-negative number n less than the length of s, output the character at the n-th position of s.

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

s will consist of printable characters only.

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

Testcases (0-indexing):

n s        output
0 "abcde"  a
1 "abcde"  b
2 "a != b" !
3 "+-*/"   /
4 "314159" 5


Testcases (1-indexing):

n s        output
1 "abcde"  a
2 "abcde"  b
3 "a != b" !
4 "+-*/"   /
5 "314159" 5


This is , so shortest answer in bytes wins.

• I downvoted this because it's​ not really a programming or golfing challenge; all that's essentially being asked here is which language has the shortest built-in​ for the job. – Shaggy May 13 '17 at 14:11
• @Shaggy Same for many other simple challenges like adding two numbers, testing if a number is prime or printing "Hello, World!". While these are boring in many languages that can do them out of the box, they can be interesting challenges for more primitive languages that have to roll their own implementation. Plus, anything more complicated is usually too much to ask of those languages, so it's nice to have an outlet for them. If trivial challenges bore you, try doing them in a nontrivial language. – Martin Ender May 13 '17 at 14:15

# Husk, 1 byte

Another built-in, 1-indexed:

!


Try it online!

## Julia 0.6.0 (13 bytes)

f(s,n)=(s[n])

s is a string, n the index. Pretty simple since Julia considers strings as arrays so I just call the index in the array. Julia is a 1 indexing.

Try it online!

# Cubically, 18 16 bytes

$:7~(~*1-1/1)6@7  (1-based index) Input format is n abcde, where n is the index, and abcde is the string. How it works: $:7              Get numeric input and set notepad to it
~             Skip next character (the space)
(       )6   Do while the notepad is nonzero
~           Get next character of input
*1-1/1     Subtract 1 from notepad (found by Kamil Drakari)
@7 Output the current input character


Try it online

# T-SQL, 29 bytes

SELECT SUBSTRING(s,n,1)FROM t


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

Rebol, Red 4 bytes

s/:n


(need to make body at least 30 to satisfy submission nanny)

• Hello and welcome to PPCG. You can use the HTML comment tag <!-- ... --> to fulfill your post length needs. Also, I would suggest using a # markdown or HTML header tag to start your post with a header. – Jonathan Frech Sep 21 '18 at 1:18

# MathGolf, 1 byte

§


Try it online!

Finally a showcase for the implicit input! Works regardless of the order of the input (if the string or int comes first).

# Acc!!, 85 bytes

N
Count i while _/128^i/32 {
_+128^(i+1)*N
}
Count j while N/48 {
_/128
}
Write _%128


Takes the string on one line of stdin and the 0-based index, in unary, on the following line. (I hope unary is okay. Decimal I/O is a pain in Acc!!) Try it online!

### Commented

# Read printable ASCII characters into the accumulator, storing the first character
# in the least significant place
N
Count i while _/128^i/32 {
_+128^(i+1)*N
}
# Read 1s, shifting the accumulator down by one character's worth each time
Count j while N/48 {
_/128
}
# We have now removed the first i characters from the original input string,
# which means the first remaining character is the one at index i (0-based)
Write _%128


# Perl 5-plF, 9 bytes

$_=$F[<>]


Try it online!

# Befunge-98 (FBBI), 5 bytes

1-indexing. Input is the index, a space and the string.

&k~,@


Try it online!

This just reads index characters as input and prints the last one.