| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Uiua | 240619T201433Z | Joonas |
| 015 | AWK F "" | 240617T194351Z | corvus_1 |
| 037 | Go | 240617T144920Z | bigyihsu |
| nan | 240617T113325Z | RARE Kpo | |
| 011 | Zsh | 240616T181920Z | GammaFun |
| 001 | Uiua SBCS | 240616T160445Z | chunes |
| 010 | Rust | 240616T152400Z | corvus_1 |
| 033 | R | 240616T144944Z | int 21h |
| 001 | Thunno 2 | 230611T061119Z | The Thon |
| 001 | Vyxal | 221012T010959Z | DialFros |
| 003 | Factor | 220114T061527Z | chunes |
| 005 | Befunge98 FBBI | 200813T100645Z | ovs |
| 002 | Integral | 200813T094852Z | user9649 |
| 009 | Perl 5 plF | 200813T070334Z | Dom Hast |
| 085 | Acc!! | 200813T054306Z | DLosc |
| 001 | MathGolf | 180921T063837Z | maxb |
| 004 | Rebol | 180921T010024Z | HappySpo |
| 029 | TSQL | 180920T202348Z | BradC |
| 016 | Cubically | 170809T211833Z | TehPers |
| 013 | Julia 0.6.0 | 170809T191830Z | Goysa |
| 012 | Java 8 | 170515T104020Z | Kevin Cr |
| 001 | Husk | 170809T103151Z | ბიმო |
| 013 | Excel | 170809T095227Z | Wernisch |
| 013 | x86 Machine Code | 170808T160314Z | Govind P |
| 001 | k kona | 170519T212850Z | Simon Ma |
| 018 | tcl | 170527T211012Z | sergiol |
| 016 | Carrot | 170513T104350Z | user4180 |
| 003 | Juby | 170520T221758Z | Cyoce |
| 009 | Ruby | 170520T221036Z | Cyoce |
| 017 | SmileBASIC 3 | 170520T183414Z | snail_ |
| 017 | brainfuck | 170520T162233Z | fireflam |
| 001 | RProgN 2 | 170520T073030Z | ATaco |
| 019 | Retina | 170513T103334Z | user4180 |
| 014 | TIBasic | 170520T035005Z | pizzapan |
| 003 | Pyth | 170519T213255Z | Maria |
| 011 | Ruby | 170519T204234Z | Alex |
| 114 | Chip | 170519T193025Z | Phlarx |
| 053 | Swift | 170513T132236Z | Mr. Xcod |
| 001 | APL Dyalog | 170515T112745Z | Adá |
| 003 | Charcoal | 170515T095741Z | ASCII-on |
| 011 | Groovy | 170514T195157Z | staticme |
| 003 | CJam | 170513T103154Z | Martin E |
| nan | 170514T182355Z | Brad Gil | |
| 018 | PowerShell | 170514T174431Z | colsw |
| 072 | LLVM IR | 170514T163629Z | Downgoat |
| 008 | Cubix | 170513T104344Z | Luke |
| 001 | Axiom | 170513T142013Z | user5898 |
| 028 | Python 3 | 170514T092005Z | user6927 |
| 011 | C# | 170514T083506Z | TheLetha |
| 061 | Axiom | 170514T063034Z | user5898 |
| 018 | Mathematica | 170514T011707Z | numberma |
| 018 | Perl 6 | 170513T180005Z | Sean |
| 003 | Fireball | 170513T165845Z | Okx |
| 002 | Brachylog | 170513T163738Z | Fatalize |
| 008 | Vim | 170513T161931Z | DJMcMayh |
| nan | ><> | 170513T111041Z | Artyer |
| 024 | QBasic 4.5 | 170513T153551Z | steenber |
| 003 | Pip | 170513T153108Z | steenber |
| 010 | JavaScript | 170513T103704Z | Stephen |
| 004 | Dyvil | 170513T151842Z | Clashsof |
| 008 | QBIC | 170513T145112Z | steenber |
| 009 | BF | 170513T130353Z | user4180 |
| 031 | sed | 170513T123652Z | eush77 |
| 022 | PHP | 170513T115831Z | Jör |
| 043 | S.I.L.O.S | 170513T114127Z | Rohan Jh |
| 003 | Clojure | 170513T104511Z | NikoNyrh |
| 009 | Turtlèd | 170513T112848Z | Destruct |
| 018 | C | 170513T112341Z | jdt |
| 004 | Haskell | 170513T110811Z | nimi |
| 004 | GolfScript | 170513T105741Z | Erik the |
| 037 | Prolog SWI | 170513T104208Z | Emigna |
| 005 | Alice | 170513T105151Z | Martin E |
| 001 | Jelly | 170513T104922Z | Erik the |
| 015 | Python | 170513T104757Z | Artyer |
| 032 | Batch | 170513T104622Z | Neil |
| 010 | Alice | 170513T103417Z | Martin E |
| 002 | Japt | 170513T103542Z | Luke |
| 001 | 05AB1E | 170513T103443Z | Emigna |
| 001 | MATL | 170513T103355Z | Stewie G |
| 010 | Octave | 170513T103243Z | Stewie G |
Uiua, 4 bytes
⊏-1
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}
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.
awk
echo '
1 "abcde"
2 "abcde"
3 "a != b"
4 "+-*/"
5 "314159"' |
gawk '$_=substr($2,$_,1)' FS=' ?"'
a
b
!
/
5
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')
Befunge-98 (FBBI), 5 bytes
1-indexing. Input is the index, a space and the string.
&k~,@
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
Explanation
r Last b
n Head
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
§
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
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.
Java 8, 18 17 12 bytes
s->s::charAt
-5 bytes saved thanks to @Jakob.
Java 7, 43 bytes
char c(String s,int n){return s.charAt(n);}
Both 0-indexed.
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//.$/
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?
,[,>+<-]>[<,>-],.
Uses 0-indexing, takes number in unary, n separated from s by .
This separates n from s with a newline.
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`.
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.
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.
APL (Dyalog), 1 byte
⌷ (Index) or ⊃ (Pick)
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
Groovy 11 bytes
{s,n->s[n]}
0-indexed.
Perl 6, 15 bytes
{$^s.comb[$^n]}
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
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.
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.
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
∋₎
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.
><>, 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.
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.
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.
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:
Input: index and the string, separated by one space. Index in unary, but zero-based.
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+].
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]
Haskell, 4 bytes
(!!)
0-based indexing. Usage example: (!!) "Hello" 1-> 'e'.
Alice, 5 bytes
I&IO@
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.
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
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.
MATL, 1 byte
)
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.