学习C#两天了,总想做点什么,想起数据结构中的排序,于是在C#中对文件进行排序,说做就做,于是有了下面的成果,经调试通过。
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace StringComp
{
class Program
{
static void Main(string[] args)
{
//定义十个文件名
string[] CompString ={"abcde", "abcdefg", "fgh4hdf", "asdfadsf","wwelkhg", "bmcvc", "fghgfjk6y", "ghguy776", "fd4r", "544ffggf"};
//循环变量
string TempString = ""; //临时保存最小值,如果有变化则
int i = 0, j = 0;
string SmallString=""; //保存当次循环最小的字符串
for (i = 0; i < 10; i++)
{
SmallString = CompString[i]; //将当前要与比较的字符串保存到SmallString中,拿SmallString与其他去比较
SmallString.Trim(); //除空格
int Result=0;
for (j =i+1; j < 10; j++)
{
Result = string.Compare(CompString[j], SmallString); //比较当前字符串与SmallString
if (Result < 0) //Comstring(j)大于smallstring
{
TempString = SmallString; //将当前最小字符串保存到临时变量中
SmallString = CompString[j]; //将当前找到的最小字符串记录下来,并保存到SmallString中
CompString[j] = TempString; //将原来SmallString中的字符串保存到当前值中
}
}
CompString[i] = SmallString; //将最小字符串保存到最前的数组中
}
Console.WriteLine("按照从小到大(数字在字母前)排序后的结果为:" + "/n");
Console.WriteLine(CompString[0] + "/n");
Console.WriteLine(CompString[1] + "/n");
Console.WriteLine(CompString[2] + "/n");
Console.WriteLine(CompString[3] + "/n");
Console.WriteLine(CompString[4] + "/n");
Console.WriteLine(CompString[5] + "/n");
Console.WriteLine(CompString[6] + "/n");
Console.WriteLine(CompString[7] + "/n");
Console.WriteLine(CompString[8] + "/n");
Console.WriteLine(CompString[9] + "/n");
}
}
}
附上在C#中的关于字符串的函数,给有需要的朋友参考:
C#中字符串操作函数
string stringMessage = string.Empty;
stringMessage .ToLower()转化成小写字母
stringMessage .ToUpper()转化成大写字母