開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第15章(文字列)15.6(練習問題)練習15-1-1.を解いてみる。
その他参考書籍
練習15-1-1.
コード
using System; class Tester { public void Run() { string s1 = "Hello"; string s2 = "World"; string s3 = @"Come visit us at http://www.LibertyAssociates.com"; string s41 = s1 + s2; string s42 = string.Concat(s1, s2); string s5 = "world"; string s61 = s3; string s62 = string.Copy(s3); string[] strs = { s1, s2, s3, s41, s42, s5, s61, s62 }; Console.WriteLine("各文字列の長さ"); foreach (string item in strs) { Console.WriteLine("{0}: {1}", item, item.Length); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
各文字列の長さ Hello: 5 World: 5 Come visit us at http://www.LibertyAssociates.com: 49 HelloWorld: 10 HelloWorld: 10 world: 5 Come visit us at http://www.LibertyAssociates.com: 49 Come visit us at http://www.LibertyAssociates.com: 49 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var s1 = "Hello", s2 = "World", s3 = "Come visit us at http://www.LibertyAssociates.com", s4 = s1 + s2, s5 = "world", s6 = s3, strs = [s1, s2, s3, s4, s5, s6], result = "各文字列の長さ\n", i, max; for (i = 0, max = strs.length; i < max; i += 1) { result += strs[i] + ": " + strs[i].length + "\n"; } $('#pre0').text(result);
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- s1 = "Hello" s2 = "World" s3 = """Come visit us at http://www.LibertyAssociates.com""" s4 = s1 + s2 s5 = "world" s6 = s3 strs = [s1, s2, s3, s4, s5, s6] print("各文字列の長さ") for s in strs: print("{0}: {1}".format(s, len(s)))
入出力結果(Terminal)
$ ./sample.py 各文字列の長さ Hello: 5 World: 5 Come visit us at http://www.LibertyAssociates.com: 49 HelloWorld: 10 world: 5 Come visit us at http://www.LibertyAssociates.com: 49 $
0 コメント:
コメントを投稿