2018年5月31日木曜日

開発環境

初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)日向 俊二 (翻訳)、オライリージャパン)の15章(文字列)、15.6(練習問題)、練習15-1.を取り組んでみる。

コード

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        string s1 = "Hello";
        string s2 = "World";
        string s3 = @"Come visit us at http://www.LibertyAssociates.com";
        string s4 = s1 + s2;
        string s5 = "world";
        string s6 = String.Copy(s3);
        string[] vs = { s1, s2, s3, s4, s5, s6 };

        Console.WriteLine("1.");
        foreach (var item in vs)
        {
            Console.WriteLine("{0}: {1}", item, item.Length);
        }
        Console.WriteLine("2.");
        foreach (var item in vs)
        {
            Console.WriteLine("{0}: {1}", item, item[2]);
        }
        Console.WriteLine("3.");
        foreach (var item in vs)
        {
            Console.Write("{0}: ", item);
            if (item.IndexOf('H') == -1)
            {
                Console.WriteLine("含まれていない。");
            }
            else
            {
                Console.WriteLine("含まれている。");
            }
        }
        Console.WriteLine("4.");
        foreach (var item in vs)
        {
            if (item == s2)
            {
                Console.WriteLine(item);
            }
        }
        Console.WriteLine("5.");
        foreach (var item in vs)
        {
            if (item.ToLower() == s2.ToLower())
            {
                Console.WriteLine(item);
            }
        }
    }
}

入出力結果(コマンドプロンプト)

1.
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
2.
Hello: l
World: r
Come visit us at http://www.LibertyAssociates.com: m
HelloWorld: l
world: r
Come visit us at http://www.LibertyAssociates.com: m
3.
Hello: 含まれている。
World: 含まれていない。
Come visit us at http://www.LibertyAssociates.com: 含まれていない。
HelloWorld: 含まれている。
world: 含まれていない。
Come visit us at http://www.LibertyAssociates.com: 含まれていない。
4.
World
5.
World
world
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿