String对象的方法属性综合运用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Camel
 6 {
 7    /// <summary>
 9    /// 将输入的字符串按Camel规则转换
10    /// </summary>
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             string name;    // 输入的原始字符串
16             string choice;  // 是否继续执行的选择
17 
18             do
19             {
20                 Console.WriteLine("请输入一个字符串,各单词以空格分隔:");
21                 name = Console.ReadLine();
22 
23                 ConvertToCamel(ref name);
24                 Console.WriteLine("按Camel规则转换后的名称是:{0}", name);
25 
26                 // 是否继续
27                 Console.Write("\n还要继续吗(Yes/No)?");
28                 choice = Console.ReadLine().Trim().ToLower();
29             } while (choice == "yes");
30             
31 
32             Console.ReadLine();
33         }
34 
35         // 按Camel规则转换
36         private static void ConvertToCamel(ref string name)
37         {
38             string[] words;    // 组成名称的各个单词数组
39             string tempWord;   // 在组合过程中的中间变量
40 
41             words = name.Split(' ');  // 以空格为分隔符分割字符串            
42 
43             name = "";
44             
45             Name = words[0].ToLower();
46             for (int i = 1; i < words.Length; i++)
47             {
48                 // 取单词的第一个字母,变成大写
49                 tempWord = words[i].Substring(01).ToUpper();
50                 name = name + tempWord;
51 
52                 // 取单词中剩下的字母,变成小写
53                 tempWord = words[i].Substring(1).ToLower();
54                 name = name + tempWord;
55             }           
56         }
57     }
58 }
posted @ 2008-10-13 15:20  Edward Xie  阅读(148)  评论(0编辑  收藏  举报