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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
9번째 줄: 9번째 줄:
==ALGOL 68==
==ALGOL 68==
[[category: ALGOL 68]]
[[category: ALGOL 68]]
<source lang="html4strict">
<syntaxhighlight lang="html4strict">
"Hello, World"[2];        // 'e'
"Hello, World"[2];        // 'e'
</source >
</syntaxhighlight >


==C++==
==C++==
[[분류: C++]]
[[분류: C++]]
{{참고|C++ .at()}}
{{참고|C++ .at()}}
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
25번째 줄: 25번째 줄:
cout << s.at(2) << endl; // r
cout << s.at(2) << endl; // r
}
}
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
<syntaxhighlight lang="csharp">
string str = "Hello, World";
string str = "Hello, World";
char ch = str[1];
char ch = str[1];
// 'e'
// 'e'
</source>
</syntaxhighlight>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
<source lang='php'>
<syntaxhighlight lang='php'>
=MID("hello",1,1)
=MID("hello",1,1)
// h
// h
44번째 줄: 44번째 줄:
=MID("hello",3,1)
=MID("hello",3,1)
// l
// l
</source>
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
String str = "Hello, World";
String str = "Hello, World";
str.charAt(2);  // 'l'
str.charAt(2);  // 'l'
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var str = "Hello, World";
var str = "Hello, World";
str.charAt(2);  // 'l'
str.charAt(2);  // 'l'
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
NSString* ch = CHAR_AT(@"A가★あ中", 2);
NSString* ch = CHAR_AT(@"A가★あ中", 2);
NSLog(@"%@", ch); // ★
NSLog(@"%@", ch); // ★
</source>
</syntaxhighlight>
<source lang='objc'>
<syntaxhighlight lang='objc'>
NSString* str = @"A가★あ中";
NSString* str = @"A가★あ中";
unichar ch = [str characterAtIndex:2];
unichar ch = [str characterAtIndex:2];
NSLog(@"%C", ch); // ★
NSLog(@"%C", ch); // ★
</source>
</syntaxhighlight>


==Pascal==
==Pascal==
[[category: Pascal]]
[[category: Pascal]]
<source lang="pascal">
<syntaxhighlight lang="pascal">
var  
var  
   MyStr: string = 'Hello, World';
   MyStr: string = 'Hello, World';
81번째 줄: 81번째 줄:
begin
begin
   MyChar := MyStr[2];        // 'e'
   MyChar := MyStr[2];        // 'e'
</source >
</syntaxhighlight >


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl'>
$str = 'Hello, World';
$str = 'Hello, World';
substr($str, 2, 1);  # 'l'
substr($str, 2, 1);  # 'l'
substr($str, -3, 1); # 'r'
substr($str, -3, 1); # 'r'
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$str = "Hello";
$str = "Hello";
$ch = $str[1];
$ch = $str[1];
// e
// e
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang="python">
<syntaxhighlight lang="python">
"Hello, World"[2]          #  'l'
"Hello, World"[2]          #  'l'
"Hello, World"[-3]        #  'r'
"Hello, World"[-3]        #  'r'
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby">
"Hello, World"[2]          #  'l'
"Hello, World"[2]          #  'l'
"Hello, World"[-3]        #  'r'
"Hello, World"[-3]        #  'r'
</source>
</syntaxhighlight>


==Smalltalk==
==Smalltalk==
[[category: Smalltalk]]
[[category: Smalltalk]]
<source lang="smalltalk">
<syntaxhighlight lang="smalltalk">
'Hello, World' at: 2.    "$e"
'Hello, World' at: 2.    "$e"
</source>
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang="vb">
<syntaxhighlight lang="vb">
GetChar("Hello, World", 2) '  "e"
GetChar("Hello, World", 2) '  "e"
</source>
</syntaxhighlight>


==VB.NET==
==VB.NET==
[[category: VB.Net]]
[[category: VB.Net]]
<source lang="vbnet">
<syntaxhighlight lang="vbnet">
"Hello, World".Chars(2)    '  "l"
"Hello, World".Chars(2)    '  "l"
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:32 판

1 개요

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

2 ALGOL 68

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

3 C++

#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
}

4 C#

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

5 Excel

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

6 Java

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

7 JavaScript

var str = "Hello, World";
str.charAt(2);  // 'l'

8 Objective-C

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

9 Pascal

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

10 Perl

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

11 PHP

$str = "Hello";
$ch = $str[1];
// e

12 Python

"Hello, World"[2]          #  'l'
"Hello, World"[-3]         #  'r'

13 Ruby

"Hello, World"[2]          #  'l'
"Hello, World"[-3]         #  'r'

14 Smalltalk

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

15 VB

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

16 VB.NET

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

17 같이 보기

18 참고