using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class stringChange : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//换行
lblOldStr.Text = "西 游 记 ,红 楼 梦 ,水 浒 传 ,三 国 演 义 ,三 国 志 ,史 记,聊 斋 ,吕 氏 春 秋";
StringBuilder str = new StringBuilder(lblOldStr.Text);
for (int i = ; i < str.Length; i++)
{
if (str[i] == ',')
str.Insert(++i, "<br />");
changedStr.InnerHtml = str.ToString();
} //颠倒输出
char[] chr = lblOldStr.Text.ToCharArray();
Array.Reverse(chr, , lblOldStr.Text.Length);
changedStr.InnerText = new StringBuilder().Append(chr).ToString(); //移除字符串中的所有空格
char[] chr1 = lblOldStr.Text.ToCharArray();
IEnumerator enum_char = chr1.GetEnumerator();
StringBuilder strSB = new StringBuilder();
while (enum_char.MoveNext())
{
strSB.Append((char)enum_char.Current!=' '?enum_char.Current.ToString():string.Empty);
}
changedStr.InnerText = strSB.ToString(); //截取字符
string path_all = @"D:\MyProject\stringChanges\Upload\edit.txt";
string path = path_all.Substring(, path_all.LastIndexOf(@"\") + );
string file_name = path_all.Substring(path_all.LastIndexOf(@"\") + , path_all.LastIndexOf(".") - path_all.LastIndexOf(@"\") - );
string file_exec = path_all.Substring(path_all.LastIndexOf(".") + , path_all.Length - path_all.LastIndexOf(".") - );
changedStr.InnerHtml += "<br /><br />文件路径:" + path + ";<br />文件名称:" + file_name + ";<br />文件扩展名:" + file_exec + ";<br />"; //获取字符串中汉字的个数
string test_temp = "西 游 记 ,红 楼 梦 ,水 浒 传 ,三 国 演 义 ,三 国 志 ,史 记,聊 斋 ,吕 氏 春 秋";
int chinese_num = ;
Regex regex = new Regex("^[\u4E00-\u9FA5]{0,}$");
for (int i = ; i < test_temp.Length; i++)
{
chinese_num = regex.IsMatch(test_temp[i].ToString()) ? ++chinese_num : chinese_num;
}
changedStr.InnerHtml += "汉字个数为:" + chinese_num; //批量替换
string txt_temp = "啊哈哈哈哈哈…";
changedStr.InnerHtml += "<br /><br />" + txt_temp;
changedStr.InnerHtml += "<br />" + txt_temp.Replace("哈", "呵") + "<br /><br />"; double value;
if (double.TryParse("", out value))
changedStr.InnerHtml += value;
else
changedStr.InnerHtml += "请输入正确的字符~!"; //对字符串进行加密与解密
changedStr.InnerHtml += "<br /><br />" + new Encrypt().ToEncrypt("", "你好");
changedStr.InnerHtml += "<br /><br />" + new Encrypt().ToDecrypt("", "ahFbgmk57JI="); //字符转ASCII码:
string str1 = "a";
byte[] bytetest = Encoding.Default.GetBytes(str1);
changedStr.InnerHtml += "<br /><br />" + bytetest.ToString(); changedStr.InnerHtml += "<br /><br />" + Asc(str1);
changedStr.InnerHtml += "<br /><br />" + Chr(); //四舍五入
double val1, val2;
if (double.TryParse("123.334444555", out val1) && double.TryParse("123.334444555", out val2))
changedStr.InnerHtml += "<br /><br />" + Math.Round(val1 + val2, ).ToString();
else
changedStr.InnerHtml += "<br /><br />" + "输入数值不正确~!"; changedStr.InnerHtml += "<br /><br />" + new PinYin().GetABC("你好"); //数字大小写转换
int iTemp;
string num="";
if (int.TryParse(num, out iTemp))
changedStr.InnerHtml += "<br /><br />" + new Upper().NumToChinese(num);
else
changedStr.InnerHtml += "<br /><br />" + "请输入正确整数数值~!"; } //字符转ASCII码:
public static int Asc(string character)
{
if (character.Length == )
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
} } public static string Chr(int asciiCode)
{
if (asciiCode >= && asciiCode <= )
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
} public class Encrypt
{
internal string ToEncrypt(string encrKey, string str)
{
try
{
byte[] byte_key = Encoding.Unicode.GetBytes(encrKey);//将密钥字符串转换为字节序列
byte[] byte_data = Encoding.Unicode.GetBytes(str); //将字符串转换为字节序列
MemoryStream Stream_MS = new MemoryStream(); //创建内存流对象
CryptoStream CS_Stream = new CryptoStream(Stream_MS, //创建加密流对象
new DESCryptoServiceProvider().CreateEncryptor(byte_key, byte_key), CryptoStreamMode.Write);
CS_Stream.Write(byte_data, , byte_data.Length);//向加密流中写入字节序列
CS_Stream.FlushFinalBlock();//将数据压入基础流
byte[] bt_temp = Stream_MS.ToArray(); //从内存流中获取字节序列
CS_Stream.Close();//关闭加密流
Stream_MS.Close();//关闭内存流
return Convert.ToBase64String(bt_temp); //方法返回加密后的字符串
}
catch (CryptographicException ce)
{
throw new Exception(ce.Message);
}
} internal string ToDecrypt(string encrKey, string str)
{
try
{
byte[] byte_key = Encoding.Unicode.GetBytes(encrKey); //将密钥字符串转换为字节序列
byte[] byte_data = Convert.FromBase64String(str); //将加密后的字符串转换为字节序列
MemoryStream Stream_MS = new MemoryStream(byte_data); //创建内存流对象并写入数据
CryptoStream CS_Stream = new CryptoStream(Stream_MS, //创建加密流对象
new DESCryptoServiceProvider().CreateDecryptor(byte_key, byte_key), CryptoStreamMode.Read);
byte[] bt_temp = new byte[];//创建字节序列对象
MemoryStream MS_temp = new MemoryStream(); //创建内存流对象
int i = ;//创建记数器
while ((i = CS_Stream.Read(bt_temp, , bt_temp.Length)) > ) //使用while循环得到解密数据
{
MS_temp.Write(bt_temp, , i); //将解密后的数据放入内存流
}
return Encoding.Unicode.GetString(MS_temp.ToArray()); //方法返回解密后的字符串
}
catch (CryptographicException ce)
{
throw new Exception(ce.Message);
}
}
} public class PinYin
{
#region
//定义拼音区编码数组
private static int[] getValue = new int[]
{
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-, -,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-
};
//定义拼音数组
private static string[] getStr = new string[]
{
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can",
"Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng",
"Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong",
"Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De",
"Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui",
"Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo",
"Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong",
"Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han",
"Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan",
"Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing",
"Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke",
"Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo",
"La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang",
"Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun",
"Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian",
"Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang",
"Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning",
"Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan",
"Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po",
"Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu",
"Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou",
"Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen",
"Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu",
"Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan",
"Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian",
"Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai",
"Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao",
"Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang",
"Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun",
"Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",
"Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan",
"Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"
};
#endregion /// <summary>
/// 将汉字转换拼音的方法
/// </summary>
/// <param name="str">汉字字符串</param>
/// <returns>拼音字符串</returns>
public string GetABC(string str)
{
Regex reg = new Regex("^[\u4e00-\u9fa5]$");//验证输入是否为汉字
byte[] arr = new byte[];//定义字节数组
string pystr = "";//定义字符串变量用于返回拼音
char[] mChar = str.ToCharArray();//获取汉字对应的字符数组
return GetStr(mChar, pystr, reg, arr);//返回获取到的汉字拼音
} private string GetStr(char[] mChar, string pystr, Regex reg, byte[] arr)
{
int asc = , M1 = , M2 = ;
for (int j = ; j < mChar.Length; j++)
{
if (reg.IsMatch(mChar[j].ToString()))//如果输入的是汉字
{
arr = System.Text.Encoding.Default.GetBytes(mChar[j].ToString());
M1 = (short)(arr[]);
M2 = (short)(arr[]);
asc = M1 * + M2 - ;
if (asc > && asc < )
{
pystr += mChar[j];
}
else
{
switch (asc)
{
case -:
pystr += "Zhen"; break;
case -:
pystr += "Qian"; break;
case -:
pystr += "Jia"; break;
case -:
pystr += "Ge"; break;
case -:
pystr += "Ga"; break;
case -:
pystr += "La"; break;
case -:
pystr += "Chen"; break;
case -:
pystr += "Mao"; break;
case -:
pystr += "Mao"; break;
case -:
pystr += "Die"; break;
case -:
pystr += "Tian"; break;
default:
for (int i = (getValue.Length - ); i >= ; i--)
{
if (getValue[i] <= asc)//判断汉字的拼音区编码是否在指定范围内
{
pystr += getStr[i];//如果不超出范围则获取对应的拼音
break;
}
}
break;
}
}
}
else//如果不是汉字
{
pystr += mChar[j].ToString();//如果不是汉字则返回
}
}
return pystr;
}
} public class Upper
{
public string NumToChinese(string x)
{
//数字转换为中文后的数组
string[] array_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
//为数字位数建立一个位数组
string[] array_digit = new string[] { "", "拾", "佰", "仟" };
//为数字单位建立一个单位数组
string[] array_units = new string[] { "", "万", "亿", "万亿" };
string str_returnValue = ""; //返回值
int finger = ; //字符位置指针
int int_m = x.Length % ; //取模
int int_k = ;
if (int_m > )
int_k = x.Length / + ;
else
int_k = x.Length / ;
//外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"
for (int i = int_k; i > ; i--)
{
int int_L = ;
if (i == int_k && int_m != )
int_L = int_m;
//得到一组四位数
string four = x.Substring(finger, int_L);
int int_l = four.Length;
//内层循环在该组中的每一位数上循环
for (int j = ; j < int_l; j++)
{
//处理组中的每一位数加上所在的位
int n = Convert.ToInt32(four.Substring(j, ));
if (n == )
{
if (j < int_l - && Convert.ToInt32(four.Substring(j + , )) > && !str_returnValue.EndsWith(array_num[n]))
str_returnValue += array_num[n];
}
else
{
if (!(n == && (str_returnValue.EndsWith(array_num[]) | str_returnValue.Length == ) && j == int_l - ))
str_returnValue += array_num[n];
str_returnValue += array_digit[int_l - j - ];
}
}
finger += int_L;
//每组最后加上一个单位:",万,",",亿," 等
if (i < int_k) //如果不是最高位的一组
{
if (Convert.ToInt32(four) != )
//如果所有4位不全是0则加上单位",万,",",亿,"等
str_returnValue += array_units[i - ];
}
else
{
//处理最高位的一组,最后必须加上单位
str_returnValue += array_units[i - ];
}
}
return str_returnValue;
}
}
交流群:225443677