參考答案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD01
{
class Program
{
static void Main(string[] args)
{
// TODO
try
{
Console.Write("請輸入第一個字串>");
string s1 = Console.ReadLine();
Console.Write("請輸入第一個字串>");
string s2 = Console.ReadLine();
Console.Write("請輸入字串連結位置(輸入-1則將字串2連接於字串1末端)>");
int location = int.Parse(Console.ReadLine());
if (location == -1)
Console.WriteLine("新字串為" + insertString(s1, s2));
else
Console.WriteLine("新字串為" + insertString(s1, s2,location));
}
catch (Exception)
{
Console.WriteLine("輸入錯誤,請重新執行");
}
Console.ReadLine();
}
// TODO
public static string insertString(string s1, string s2, int loc)
{
if (loc == 0)
return s2 + s1;
else
return s1.Substring(0, loc)+s2+s1.Substring(loc);
}
public static string insertString(string s1, string s2)
{
return s1 + s2;
}
}
}
執行結果:
留言列表