Asp.net 字符(二)
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
Asp.net 字符(二)的更多相关文章
- 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递
通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上,因此需要确保您是否掌握了上一节的内容.本章的目标是在今天学习结束时利用最佳实践解决方案创建一个小型的MV ...
- asp.net生成二维码的方法
asp.net生成二维码的方法 [复制链接] 这个要利用一个Dll文件. 如下 Gma.QrCodeNet.Encoding.dll (105.5 KB, 下载次数: 27) 当然大家也可以直 ...
- ASP入门(二十二)-连接数据库
ADO 简介 在 ASP 中访问数据库使用的 ADO 组件. ADO 是一项微软的技术. ADO 指 ActiveX 数据对象(ActiveX Data Objects). ADO 是微软的 Acti ...
- ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段
ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...
- ASP 三十二条精华代码 (1)
ASP 三十二条精华代码 (1) 2009-08-10 09:53:03 www.hackbase.com 来源:互联网 1. oncontextmenu="window.event.r ...
- 彻底解密C++宽字符(二)
彻底解密C++宽字符(二) 转:http://club.topsage.com/thread-2227977-1-1.html 4.利用codecvt和use_facet转换 locale和facet ...
- 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 【转】
http://www.cnblogs.com/powertoolsteam/p/MVC_two.html 通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上 ...
- ASP截取字符 截取字符之间的字符
ASP截取字符:MID函数Mid(变量或字串符,开始字节, 结尾字节(可不填)) InStrRev(变量, "字串符") 最后出现位置InStr(变量, "字串符&qu ...
- Linux 安装Mono环境 运行ASP.NET(二)
一.安装libgdiplus 前面我们已经安装了apr.apr_util.pcre和httpd apache .现在我们来安装libgdiplus Libgdiplus是一个Mono库,用于对 ...
随机推荐
- int跟byte[]数组互转的方法,整数 + 浮点型
整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...
- IOS开发 应用程序图标数字角标
其实实现这个功能很简单,只要调用UIApplication即可. 用法用例:[UIApplication sharedApplication].applicationIconBadgeNumber ...
- System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...
- Java对象创建阶段的代码调用顺序
在创建阶段系统通过下面的几个步骤来完成对象的创建过程 为对象分配存储空间 开始构造对象 从超类到子类对static成员进行初始化 超类成员变量按顺序初始化,递归调用超类的构造方法 子类成员变量按顺序初 ...
- Hibernate一些防止SQL注入的方式
Hibernate在操作数据库的时候,有以下几种方法来防止SQL注入,大家可以一起学习一下. 1.对参数名称进行绑定: Query query=session.createQuery(hql); qu ...
- linux系统和依赖包常用下载地址
http://ftp.gnome.org/pub/gnome/sources/gstreamer/0.10/ http://www.linuxfromscratch.org/blfs/view/svn ...
- 网络存储-Samba、NAS---未知的用户名或错误密码
项目中的文件需要保存到网络存储设备中,之前用的是NAS.因没来得及采购就先用Samba顶上.代码发现通用…… 一.定义: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器 ...
- Mac OSX Versions输入username按1下都会出现2个字符,并且不能create,解决方法
我的系统,安装的versions1.3.2,下载地址:http://www.jb51.net/softs/193467.html 安装好了以后Versions输入username按1下都会出现2个字符 ...
- Log4net对文件的支持
RollingFileAppender循环记录日志(指定文件最大长度) <appender name="RollingFileAppender" type="log ...
- oracle/sqlserver 递归
1.Oracle递归查询 2.SqlServer递归查询 with cte as ( select t.id,t.name,t.parentId from dbo.Department t where ...