"함수 CharAt()"의 두 판 사이의 차이

57번째 줄: 57번째 줄:
     s := "hello"
     s := "hello"
     fmt.Println(s[1])
     fmt.Println(s[1])
     fmt.Println(s[5])
     fmt.Println(string(s[1]))
    fmt.Printf("%c\n", s[1])
}
}
</syntaxhighlight>
</syntaxhighlight>

2022년 3월 10일 (목) 11:33 판

1 개요

charAt
characterAtIndex
  • Returns character at index in the string.
  • Equivalent: See substring of length 1 character.

2 ALGOL 68

html
Copy
"Hello, World"[2];         // 'e'

3 C++

C++
Copy
#include <iostream>
using namespace std;
int main() {
	string s = "World";
	cout << s.at(0) << endl; // W
	cout << s.at(1) << endl; // o
	cout << s.at(2) << endl; // r
}
Loading

4 C#

C#
Copy
string str = "Hello, World";
char ch = str[1];
// 'e'

5 Excel

PHP
Copy
=MID("hello",1,1)
// h
=MID("hello",2,1)
// e
=MID("hello",3,1)
// l

6 Go

Go
Copy
package main

import "fmt"

func main() {
    s := "hello"
    fmt.Println(s[1])
    fmt.Println(string(s[1]))
    fmt.Printf("%c\n", s[1])
}
Loading

7 Java

Java
Copy
String str = "Hello, World";
str.charAt(2);  // 'l'

8 JavaScript

JavaScript
Copy
var str = "Hello, World";
console.log( str.charAt(7) );  // W
W 

9 Objective-C

Objective-C
Copy
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
NSString* ch = CHAR_AT(@"A가★あ中", 2);
NSLog(@"%@", ch); // ★
Objective-C
Copy
NSString* str = @"A가★あ中";
unichar ch = [str characterAtIndex:2];
NSLog(@"%C", ch); // ★

10 Pascal

pascal
Copy
var 
  MyStr: string = 'Hello, World';
  MyChar: Char;
begin
  MyChar := MyStr[2];         // 'e'

11 Perl

Perl
Copy
$str = 'Hello, World';
substr($str, 2, 1);  # 'l'
substr($str, -3, 1); # 'r'

12 PHP

PHP
Copy
$str = "Hello";
echo $str[1]; // e
Loading

13 Python

Python
Copy
print( "Hello, World"[2]  )  # l
print( "Hello, World"[-3] )  # r
Loading

14 Ruby

Ruby
Copy
print "Hello, World"[2]   # l
print "Hello, World"[-3]  # r
Loading

15 Smalltalk

smalltalk
Copy
'Hello, World' at: 2.    "$e"

16 VB

vbnet
Copy
GetChar("Hello, World", 2) '  "e"

17 VB.NET

vbnet
Copy
"Hello, World".Chars(2)    '  "l"

18 같이 보기

19 참고