g | x | w | all
Bytes Lang Time Link
004Uiua240619T201433ZJoonas
015AWK F ""240617T194351Zcorvus_1
037Go240617T144920Zbigyihsu
nan240617T113325ZRARE Kpo
011Zsh240616T181920ZGammaFun
001Uiua SBCS240616T160445Zchunes
010Rust240616T152400Zcorvus_1
033R240616T144944Zint 21h
001Thunno 2230611T061119ZThe Thon
001Vyxal221012T010959ZDialFros
003Factor220114T061527Zchunes
005Befunge98 FBBI200813T100645Zovs
002Integral200813T094852Zuser9649
009Perl 5 plF200813T070334ZDom Hast
085Acc!!200813T054306ZDLosc
001MathGolf180921T063837Zmaxb
004Rebol180921T010024ZHappySpo
029TSQL180920T202348ZBradC
016Cubically170809T211833ZTehPers
013Julia 0.6.0170809T191830ZGoysa
012Java 8170515T104020ZKevin Cr
001Husk170809T103151Zბიმო
013Excel170809T095227ZWernisch
013x86 Machine Code170808T160314ZGovind P
001k kona170519T212850ZSimon Ma
018tcl170527T211012Zsergiol
016Carrot170513T104350Zuser4180
003Juby170520T221758ZCyoce
009Ruby170520T221036ZCyoce
017SmileBASIC 3170520T183414Zsnail_
017brainfuck170520T162233Zfireflam
001RProgN 2170520T073030ZATaco
019Retina170513T103334Zuser4180
014TIBasic170520T035005Zpizzapan
003Pyth170519T213255ZMaria
011Ruby170519T204234ZAlex
114Chip170519T193025ZPhlarx
053Swift170513T132236ZMr. Xcod
001APL Dyalog170515T112745ZAdá
003Charcoal170515T095741ZASCII-on
011Groovy170514T195157Zstaticme
003CJam170513T103154ZMartin E
nan170514T182355ZBrad Gil
018PowerShell170514T174431Zcolsw
072LLVM IR170514T163629ZDowngoat
008Cubix170513T104344ZLuke
001Axiom170513T142013Zuser5898
028Python 3170514T092005Zuser6927
011C#170514T083506ZTheLetha
061Axiom170514T063034Zuser5898
018Mathematica170514T011707Znumberma
018Perl 6170513T180005ZSean
003Fireball170513T165845ZOkx
002Brachylog170513T163738ZFatalize
008Vim170513T161931ZDJMcMayh
nan><>170513T111041ZArtyer
024QBasic 4.5170513T153551Zsteenber
003Pip170513T153108Zsteenber
010JavaScript170513T103704ZStephen
004Dyvil170513T151842ZClashsof
008QBIC170513T145112Zsteenber
009BF170513T130353Zuser4180
031sed170513T123652Zeush77
022PHP170513T115831ZJör
043S.I.L.O.S170513T114127ZRohan Jh
003Clojure170513T104511ZNikoNyrh
009Turtlèd170513T112848ZDestruct
018C170513T112341Zjdt
004Haskell170513T110811Znimi
004GolfScript170513T105741ZErik the
037Prolog SWI170513T104208ZEmigna
005Alice170513T105151ZMartin E
001Jelly170513T104922ZErik the
015Python170513T104757ZArtyer
032Batch170513T104622ZNeil
010Alice170513T103417ZMartin E
002Japt170513T103542ZLuke
00105AB1E170513T103443ZEmigna
001MATL170513T103355ZStewie G
010Octave170513T103243ZStewie G

Uiua, 4 bytes

⊏-1 

Try it on Uiua Pad!

There is supposed to be a whitespace after "1", but it it doesn't seem to show on this post. Also, are precharacters allowed in the output? Such as "@" which occus in all of the outputs of this code.

AWK -F "", 18 15 bytes

-3 bytes thanks to Marius_Couet

x&&$0=$x;{x=$0}

Attempt This Online!

Takes input as newline-seperated index string. Uses 1-based indexing.

Explanation:

The flag -F sets the variable FS to the empty string, which will split each line input into characters, available as $1, $2 etc.

x&&$0=$x; # if the variable x is truthy, assign the x-th char to $0
# implicitly print $0
{x=$0}    # assign x the whole line. This will be useless for the second line, but harmless.

Go, 37 bytes

func(s string,i int)byte{return s[i]}

Attempt This Online!

awk

echo '
    1 "abcde"
    2 "abcde"
    3 "a != b"
    4 "+-*/"
    5 "314159"' | 

gawk '$_=substr($2,$_,1)' FS=' ?"' 

a
b
!
/
5

Zsh, 11 bytes

<<<${2[$1]}

Try it online!

Uiua SBCS, 1 byte

Try it!

Body must be at least 30 characters; you entered 28.

Rust, 10 bytes

<[_]>::get

Can be used like so:

let f: fn(&[u8], usize) -> Option<&u8> = <[_]>::get;
f(b"xyz", 1) // results in Some(b'y')

R, 33 bytes

\(x,n)readChar(charToRaw(x)[n],1)

Attempt This Online!

slightly less boring than substr(x,n,1)

Thunno 2, 1 byte

i

Attempt This Online!

Built-in.

Vyxal, 1 byte

i

Try it Online!

Can't get much simpler than this.

Factor, 3 bytes

nth

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.

Integral, 2 Bytes

Integral doesn't have an a[n] built-in. Unfortunately it does have some slicing built-ins, which makes a 2-byter possible...

rn

Try it!

Explanation

r   Last b
 n  Head

Perl 5 -plF, 9 bytes

$_=$F[<>]

Try it online!

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

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

Rebol, Red 4 bytes

s/:n

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

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.

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

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!

Java 8, 18 17 12 bytes

s->s::charAt

-5 bytes saved thanks to @Jakob.

Try it here.

Java 7, 43 bytes

char c(String s,int n){return s.charAt(n);}

Try it here.

Both 0-indexed.

Husk, 1 byte

Another built-in, 1-indexed:

!

Try it online!

Excel, 13 bytes

=MID(B1,A1,1)

1-indexed n in A1. Text in B1.

x86 Machine Code, 13 bytes

8B 44 24 04 03 44 24 08 8A 08 8A C1 C3

Which disassembles to:

  00000000: 8B 44 24 04        mov         eax,dword ptr [esp+4]
  00000004: 03 44 24 08        add         eax,dword ptr [esp+8]
  00000008: 8A 08              mov         cl,byte ptr [eax]
  0000000A: 8A C1              mov         al,cl
  0000000C: C3                 ret

k (kona), 0 bytes (noncompeting); 1 byte

In K, list access (as function application) is implicit. As such, we can do

q)"abcdef"0 
"a" 

But that's not really in the spirit of things, so we'll make the application of 0 on "abcdef" explicit for 1 byte:

q)"abcdef"@0
"a"

tcl, 18

string index $s $n

Input is on the variables s and n

Output is the return of the script

demo — Iterates a list with all test case pairs and outputs them to console.

Carrot, 16 bytes

$^//.{#}/S1//.$/

Try it online!

The input format is as such:

string
index

And the program is 1-indexed.

Explanation

Carrot has several global variables, one for each type: string, float and array (others to be implemented soon). The program starts in string-mode, where all the operators will affect the global string variable. And I call these variables the "stack".

(Example input: abcdef\n3)

$                          Get the first line of the input and set the stack-string to this value
^                          Exit caret-mode
                           stack-string = "abcdef"
/                          Operator (behaves differently depending on the argument)
 /.{#}/                     And the argument to this operator is a regex, so this program gets the matches of this regex into the stack-array
  .                          Any character
   {#}                       Pops a line from the input. So now this evaluates to # of any character where # is the second line of the input (in this case, 3)
                           stack-array = ["abc"]
                           And now we just need to get the last character in this string, but first
S1                         Join the array on the number 1 and set this to the stack-string. Because the array only contains one element, the number 1 does not appear in the stack-string.
                           stack-string = "abc"
/                          Operator; because the argument is a regex, this retrieves the matches of the regex:
 /.$/                       Get the last character in the string
                           stack-array = ["c"]

Now this returns a one element array containing a string of length one, but it is shown as a string in the website.

If we really wanted to give the result as a string, we could easily do S"," at the end, but it doesn't matter because the output still looks the same on the interpreter.

J-uby, 3 bytes

:[]

[] is the subscripting operator. Example run:

irb(main):001:0> f = :[]
irb(main):002:0> f.call("string", 2)
=> "r"

Ruby, 9 bytes

proc &:[]

How Ruby handles operator overloading is :+, :-, :*, :[], etc. are defined as methods, so 1+2 is essentially 1.+(2). :[] is the symbol for the subscripting "operator", which when converted to a proc calls the :[] method with the first as a receiver and the second as an argument. E.g., (proc &:[]).call("asd",0) == "asd".[](0) == "asd"[0]

SmileBASIC 3, 17 bytes

Woo! String indexing!

DEF A S,N?S[N]END

Defines a command A to print the result. Uses 0-indexing. Call like A "string",number.

No string functions, 29

Here's one that uses absolutely no string-specific built-ins.

DEF A S,N
COPY T$,S,N,1?T$END

COPY is a memory copy command, capable of copying data between two compatible types. It's usually used for arrays, but it works on strings as well.

brainfuck, 17 bytes

...because why not?

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

Try it online!

Uses 0-indexing, takes number in unary, n separated from s by .

This separates n from s with a newline.

RProgN 2, 1 byte

ü

Just a builtin!

Try it online!

Retina, 28 20 19 bytes

Saved 8 bytes thanks to @MartinEnder by not using balancing groups

Saved 1 byte thanks to @mbomb007 by using ^.+ instead of ^\d+

^.+
$*
+`1¶.
¶
!1`.

Try it online!

The program is 0-indexed.

TI-Basic, 14 bytes

Prompt Str0,N
sub(Str0,N,1

Returns the Nth 1-indexed character of the string.

Noncompeting 10 bytes:

Prompt N
sub(Ans,N,1

run with: "string":prgmNAME then input the number. I can't see the upvote/downvote ratio of this meta post on whether different input types are allowed, and it doesn't say in the tag-wiki, so I'm assuming they aren't.

Pyth - 3 bytes

@wE

Try it here

Ruby, 11 bytes

->n,s{s[n]}

Try it online!

Chip, 114 bytes

*Z
,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?

S{*

This suppresses output for all bytes, by default. If powered, this can be overridden. It is a not-gate based on an xor-gate ({).

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 unsuppress output for that character, meaning that it is printed.

Swift - 53 bytes

func f(a:String,i:Int){print(Array(a.characters)[i])}

A function f that can be used as f(a: "abcdef",i: 3). It is 0-indexed.

Check it out!

APL (Dyalog), 1 byte

(Index) or (Pick)

Try them online!

They only differ for higher rank arrays and nested arrays:

2⌷A will choose the second element or second row, leaving any nesting
2⊃A will pick the second element, and attempt to remove one level of nesting

2 3⌷A will choose the element at row 2 column 3
2 3⊃A will pick the third element of the second element

(⊂2 3)⌷A will choose the second and third elements or rows
(⊂2 3)⊃A will pick the element at row 2 column 3

Charcoal, 3 bytes

§SN

Explanation

§     At index
 S   Input string
   N Input number

Try it online!

Groovy 11 bytes

{s,n->s[n]}

0-indexed.

CJam, 3 bytes

{=}

Try it online!

= is the built-in for list/string indexing.

Perl 6, 15 bytes

{$^s.comb[$^n]}

Try it

Expanded:

{ # bare block lambda with placeholder parameters 「$n」 and 「$s」

  $^s      # declare second parameter
  .comb\   # split it into graphemes
  [ $^n ]  # get the value indexed by the first parameter
}

PowerShell, 18 Bytes

param($s,$i)$s[$i]

indexes into param 1 ($s) using param 2 ($i)

LLVM IR, 72 bytes

define i8*@a(i8*,i64){
entry:%2=getelementptr i8,i8*%0,i64%1
ret i8*%2
}

Takes a (string, index) (0-indexed) as argument, where string is a c-string. Returns an i8* (char*) which points to the desired char. I can't directly move the getelementptr with the ret instruction as %0 and %1 are function-local and therefore need their own instruction

Try it online!

Cubix, 8 bytes

t@poIA//

This solution is 1-indexed. The input should consist of a number first, then a separator (that is not a digit or a .) and then the string.

Try it online!

Axiom, 1 byte

.

1-Indexing... string.index is the value of string at index some test

(5) -> "123".6
   >> Error detected within library code:
   index out of range
    protected-symbol-warn called with (NIL)

(5) -> "123".0
   >> Error detected within library code:
   index out of range
protected-symbol-warn called with (NIL)

(5) -> ["123".i  for i in 1..3]
   (5)  [1,2,3]
                                                     Type: List Character

Where you see Axiom warn for index out of space too

Python 3, 28 bytes

print(input()[int(input())])

C#, 11 bytes

s=>n=>s[n];

Axiom,61 bytes

f(s:String,n:PI):Union(Character,Complex INT)==(n>#s=>%i;s.n)

1-Indexing; it return the required char or it return the %i=sqrt(-1) for error if n> lenght(s) Some test

(4) -> [f("123",i) for i in 1..10]
   (4)  [1,2,3,%i,%i,%i,%i,%i,%i,%i]
                              Type: List Union(Character,Complex Integer)
(5) -> f("123", 0)
   Conversion failed in the compiled user function f .
   Cannot convert from type Integer to PositiveInteger for value
   0

instead of doing less (few characters) instead of do more (more cicle) instead to do fast... to do the right

Mathematica, 18 bytes

#~StringTake~{#2}&

Basic solution, but unfortunately the function name is quite long.

Perl 6, 18 bytes

{substr $^a,$^b,1}

I'm a little surprised it can't be done shorter. By me, anyway.

Fireball, 3 bytes

1-indexed.

╡1╞

Explanation:

╡         Get the first n characters of the input
 1╞       Get the last character of the that

Alternatively, for 4 bytes:

╡r↔♥

Explanation:

╡      Get the first n characters of the input
 r     Reverse
  ↔♥   Get the first character

Brachylog, 2 bytes

∋₎

Try it online!

Explanation

unifies its output with an element of the Input. With as subscript, it will unify its output with the Ith element of S, with [S,I] as input.

Vim, 8 bytes

DJ@"|xVp

Since V is backwards compatible, you can Try it online!

><>, 13 + 1 = 14 bytes

+1 for -v flag to take input

:?!\i~1-
io;\

Thanks to @steenbergh for notifying me about the -v flag and saving me 3 bytes!

Input the index with the command line argument -v [index] (0-indexed) and input the string through stdin.

Try it online!

Explanation

The stack starts with the index on top.
: duplicates it.
? ignores the next character if the index is 0. (Popping it off the stack)
If it is zero, \ reflects the direction to go down. Then, it is reflected to the right with the next \. It wraps around and executes input a character, output it and ; halts execution.
If not, ! skips the next instruction, so it doesn't go down.
i~ inputs a character and then immediately discards it.
1 pushes 1.
- subtractes 1 from the index, so one character in the input is consumed and the index is decremented. The program loops around back to the :.

QBasic 4.5, 24 bytes

INPUT a$,b:?MID$(a$,b,1)

Pretty straightforward.

Pip, 3 bytes

a@b

This is 0-based. Try it online!

JavaScript, 11 10 bytes

s=>n=>s[n]

Uses 0-based indexing.

-1 byte thanks to @Leaky Nun

f=
s=>n=>s[n]
console.log(f("abcde")(0));
console.log(f("abcde")(1));
console.log(f("a != b")(2));
console.log(f("+-*/")(3));
console.log(f("314159")(4));

Dyvil, 4 Bytes

_[_]

Creates an anonymous function that takes a String and an int and returns a char.

Usage:

let f: (String, int) -> char = _[_]
print f("abc", 1) // b

QBIC, 8 bytes

?_s;,:,1

Explanation:

?     PRINT
_s    Substring(in$, start, length)
  ;   Reads a string from the cmd line as in$
  ,:  Reads an int from the command line as start
  ,1  One character
      Statement delimeter | is auto-added at EOF.

This is 1-based. It uses QBIC's new ability to inline ; and :. Previously, to get s and n, you needed to do this:

;:?_sA,a,1

That would create the assignment of a cmd line arg to A$ and a resp. Now we can skip assignment: the interpreter extracts the assignment and moves those to the top of the script.

BF, 9 bytes

,[->,<]>.

The index is taken via the character code of a char (like the Alice submission). Following that, we have the string.

Try it online!

The TIO link uses a Bash wrapper and the input can be changed in the header file (the reason for the wrapper is so that we can see the bytes).

sed, 31 bytes

:
/^1/{s:1 .: :;b}
s: (.).*:\1:

Try it online!

Input: index and the string, separated by one space. Index in unary, but zero-based.

PHP, 22 Bytes

<?=$argv[1][$argv[2]];

Try it online!

S.I.L.O.S, 43 bytes

loadLine
readIO
a=256+i
a=get a
printChar a

Try it online!

Simple enough.

Clojure, 3

nth

:P What can you do when there is a built-in for this? This works on lists, vectors, strings and sequences. It is either O(1) or O(n), depending on the used datatype.

Turtlèd, 9 bytes

!?:[*l+].

Try it online!

explanation:

First off, Turtlèd is grid based, with a turtle thing. There are grid cells the turtle can move left, up, down, and right on, and can write things on the grid cells.

[the first grid cell the turtle starts on is marked with a *]
!         input the string into the string variable

 ?        input the number into the number variable

  :       this command takes the number variable and moves right that many.
          hence this moves right by the amount inputted

   [*  ]  this is an `until` loop. the * there means that `until` the turtle ends the loop
          on a grid cell with * written on it (that is, the first cell), it will execute
          the code inside again and again

     l+   the code inside the while loop. the `l` moves the turtle left, and the +
          increments the string pointer. the string pointer is used with the string var;
          when you want to write something from the string, you use `.`, which writes
          the pointed char. the pointed char is the n-th character of the string, n being
          the value of the string pointer. this code will execute until the l moves
          the turtle back on to the origin cell. since we moved right by the number
          inputted, this will increase the string pointer (which starts at 1)
          by the amount inputted.

       .  write the pointed char, which was dealt with in the previous comment.
          if 0 is inputted, turtle stayed on the origin square, and executed none
          of the loop, and turtle writes the first char of string input.
          if 1 is inputted, turtle moved one right, moved one left and incremented
          string pointer once, which means the second char is written. and so on.


          [the char of input has been written over the origin square]
          [implicitly the grid is outputted, which has spaces and blank lines taken out]
          [this is the requested char outputted, plus an unavoidable trailing newline
          due to how I made the interpreter. sue me]

C, 18 Bytes

#define f(s,n)s[n]

Try it online

Haskell, 4 bytes

(!!)

0-based indexing. Usage example: (!!) "Hello" 1-> 'e'.

GolfScript, 7 4 bytes

~1/=

Try it online!

I suck in GolfScript, so I value Martin Ender's help.

Prolog (SWI), 40 37 bytes

q(L,N,A):-nth0(N,L,X),char_code(A,X).

Try it online!

Alice, 5 bytes

I&IO@

Try it online!

As usual it's much shorter if we avoid Ordinal mode and use an abysmal input format. Here, the first character's code point is used as the integer input. The remainder of the input is the string. The index is 1-based.

Explanation

I   Read a character and push its code point.
&I  Read that many more characters and push them.
O   Output the last one we read.
@   Terminate the program.

Jelly, 1 byte

Try it online!

Python, 15 bytes

str.__getitem__

or

lambda s,i:s[i]

Both take 2 arguments: the string and the index. 0-indexed.

Batch, 32 bytes

@set/ps=
@call echo(%%s:~%1,1%%

Reads s from STDIN and takes n as a command-line parameter.

Alice, 10 bytes

/@!O?]&
 I

Try it online!

Expects the string on the first line and the 0-based index on the second line.

Explanation

Despite its wealth of built-ins, string indexing doesn't exist in Alice. The reason is that it requires both an integer and a string parameter, and all commands in Alice are strictly integers to integers or strings to strings.

In general, the main way to perform any operations on strings that require or result in integers is to store the string on the tape in Ordinal mode, which you can then manipulate with integers in Cardinal mode.

/    Reflect to SE. Switch to Ordinal. While in Ordinal mode, the IP bounces
     diagonally up and down through the program.
I    Read one line from STDIN and push it.
!    Store the string on the tape. This writes the character codes into consecutive
     tape cells starting at the tape head. (It also writes a -1 at the end as a
     terminator, but the tape is initialised to -1s anyway).

     The next few commands are junk that luckily doesn't affect the program:

?      Load the string from the tape and push it to the stack again.
&      Fold the next command over this string. That is, for each character
       in the string, push that character, then execute the command.
?      So we're folding "load" over the string itself. So if the string is "abc"
       we'll end up with "a" "abc" "b" "abc" "c" "abc" on the stack.
!      Store the top copy of "abc" on the tape (does nothing, because it's
       already there).

     Now the relevant part of the program continues.

I    Read another line from STDIN, i.e. the string representation of the index.
/    Reflect to W. Switch to Cardinal. The IP wraps to the last column.
&    Implicitly convert the string to the integer value it represents and
     repeat the next command that many times.
]    Move the tape head right that many cells. Note that Ordinal and Cardinal
     mode have separate tape heads, but they are initialised to the same cell.
?    Load the value from that cell, which is the code point of the character
     at the given index.
O    Print the corresponding character.
!    This discards or converts some of the strings on the stack and writes some
     value back to the tape, but it's irrelevant.
@    Terminate the program.

Japt, 2 bytes

gV

This solution is 0-indexed.

Try it online!

05AB1E, 1 byte

0-indexed.

è

Try it online!

MATL, 1 byte

)

Try it online!

Well, hard to make it much simpler. ) indexes the first input, using the second input value. This is 1-indexed.

Octave, 10 bytes

@(s,n)s(n)

Takes a string s, and a number n as input, and returns the nth character of s.