- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace DelegateApp
- {
- delegate string strDelegate(string str);
- class Program
- {
- //delegate string strDelegate(string str);
- static string strReplace(string s)
- {
- return s.Replace(' ', '_');
- }
- static string strRemove(string s)
- {
- /*
- string temp = "";
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] != ' ')
- {
- temp += s[i];
- }
- }
- return temp;
- */
- StringBuilder sb = new StringBuilder(s.Length);
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] != ' ')
- {
- sb.Append(s[i]);
- }
- }
- return sb.ToString();
- }
- static string strReverse(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- throw new ArgumentException("参数不合法");
- }
- StringBuilder sb = new StringBuilder(str.Length);
- for (int index = str.Length - 1; index >= 0; index--)
- {
- sb.Append(str[index]);
- }
- return sb.ToString();
- }
- static void Main(string[] args)
- {
- strDelegate strD;
- string str;
- strD = new strDelegate(strRemove);
- str = strD("asfa asdf asdf asdf asdf");
- Console.WriteLine(str);
- strD = new strDelegate(strReplace);
- str = strD("asdf asdf");
- Console.Out.WriteLine(str);
- strD = new strDelegate(strReverse);
- str = strD("asdf");
- Console.Out.WriteLine(str);
- }
- }
- }
- /*
- asfaasdfasdfasdfasdf
- asdf_asdf
- fdsa
- 请按任意键继续. . .
- */