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

(새 문서: ==JavaScript== <source lang='JavaScript'> var str = "HELLO WORLD"; console.log( str.charAt(0) ); // H </source>)
 
 
(사용자 2명의 중간 판 24개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: 다언어]]
[[category: string]]
==개요==
;charAt
;characterAtIndex
*Returns character at index in the string.
*Equivalent: See [[substring]] of length 1 character.


==ALGOL 68==
[[category: ALGOL 68]]
<syntaxhighlight lang="html4strict">
"Hello, World"[2];        // 'e'
</syntaxhighlight >
==C++==
[[분류: C++]]
{{참고|C++ .at()}}
<syntaxhighlight lang='cpp' run>
#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
}
</syntaxhighlight>
==C#==
[[category: Csharp]]
<syntaxhighlight lang="csharp">
string str = "Hello, World";
char ch = str[1];
// 'e'
</syntaxhighlight>
==Excel==
[[category: Excel]]
<syntaxhighlight lang='php'>
=MID("hello",1,1)
// h
=MID("hello",2,1)
// e
=MID("hello",3,1)
// l
</syntaxhighlight>
==Go==
[[분류: Go]]
{{참고|Go charAt()}}
<syntaxhighlight lang='go' run>
package main
import "fmt"
func main() {
s := "Hello, 世界"
fmt.Println(s[0])        // 72
fmt.Println(string(s[0])) // H
fmt.Printf("%c\n", s[0])  // H
}
</syntaxhighlight>
==Java==
[[category: Java]]
<syntaxhighlight lang='java'>
String str = "Hello, World";
str.charAt(2);  // 'l'
</syntaxhighlight>


==JavaScript==
==JavaScript==
<source lang='JavaScript'>
[[category: JavaScript]]
var str = "HELLO WORLD";
<syntaxhighlight lang='javascript' run>
console.log( str.charAt(0) );
var str = "Hello, World";
// H
console.log( str.charAt(7) );  // W
</source>
</syntaxhighlight>
 
==Objective-C==
[[category: Objective-C]]
<syntaxhighlight lang='objc'>
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
NSString* ch = CHAR_AT(@"A가★あ中", 2);
NSLog(@"%@", ch); // ★
</syntaxhighlight>
<syntaxhighlight lang='objc'>
NSString* str = @"A가★あ中";
unichar ch = [str characterAtIndex:2];
NSLog(@"%C", ch); // ★
</syntaxhighlight>
 
==Pascal==
[[category: Pascal]]
<syntaxhighlight lang="pascal">
var
  MyStr: string = 'Hello, World';
  MyChar: Char;
begin
  MyChar := MyStr[2];        // 'e'
</syntaxhighlight >
 
==Perl==
[[category: Perl]]
<syntaxhighlight lang='perl'>
$str = 'Hello, World';
substr($str, 2, 1);  # 'l'
substr($str, -3, 1); # 'r'
</syntaxhighlight>
 
==PHP==
[[category: PHP]]
<syntaxhighlight lang='php' run>
$str = "Hello";
echo $str[1]; // e
</syntaxhighlight>
 
==Python==
[[category: Python]]
<syntaxhighlight lang="python" run>
print( "Hello, World"[2]  )  # l
print( "Hello, World"[-3] )  # r
</syntaxhighlight>
 
==Ruby==
[[category: Ruby]]
<syntaxhighlight lang="ruby" run>
print "Hello, World"[2]  # l
print "Hello, World"[-3]  # r
</syntaxhighlight>
 
==Smalltalk==
[[category: Smalltalk]]
<syntaxhighlight lang="smalltalk">
'Hello, World' at: 2.    "$e"
</syntaxhighlight>
 
==VB==
[[category: VB]]
<syntaxhighlight lang="vb">
GetChar("Hello, World", 2) '  "e"
</syntaxhighlight>
 
==VB.NET==
[[category: VB.Net]]
<syntaxhighlight lang="vbnet">
"Hello, World".Chars(2)    '  "l"
</syntaxhighlight>
 
==같이 보기==
*[[utf8 charAt]]
*[[substr]]
*[[charCodeAt]]
 
==참고==
*http://eqcode.com/wiki/CharAt

2022년 3월 10일 (목) 11:35 기준 최신판

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 Go[ | ]

package main

import "fmt"

func main() {
	s := "Hello, 世界"
	fmt.Println(s[0])         // 72
	fmt.Println(string(s[0])) // H
	fmt.Printf("%c\n", s[0])  // H
}

7 Java[ | ]

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

8 JavaScript[ | ]

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

9 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); // ★

10 Pascal[ | ]

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

11 Perl[ | ]

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

12 PHP[ | ]

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

13 Python[ | ]

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

14 Ruby[ | ]

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

15 Smalltalk[ | ]

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

16 VB[ | ]

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

17 VB.NET[ | ]

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

18 같이 보기[ | ]

19 참고[ | ]