字符串 - string

/*
 * string的学习
 * 
 * 正则表达式:
 * 比如说:账号验证(包含英文字母,特殊符号,数字,6位以上长度)
 * 邮箱验证
 * 电话验证
 * 
 * 
*/
using System;
using System.Text.RegularExpressions;

namespace 字符串常见API {
	
	class MainClass {

		/// <summary>
		/// 获取字符串长度
		/// </summary>
		public void Test1(){
			//无论英文,特殊符号还是中文,长度都是1个字节.
			string s = "你好中国xxx,";
			Console.WriteLine("字符串长度为:" + s.Length);
		}

		/// <summary>
		/// 字符串中查找字符串
		/// </summary>
		public void Test2(){
			//返回-1证明查不到
			//如果能查到,返回的是该字符或者字符串在字符串中索引位置
			string s = "就是HFDJasdgajSGDasjgdhagshjdg不方便大家就卡死的";
			int i = s.IndexOf('F',3,1);
			Console.WriteLine (i);
		}

		/// <summary>
		/// 字符串提取,截取指定范围内的字符串
		/// </summary>
		public void Test3(){
			string s = "你好吗,我的母亲中国好棒!";
			string temp = s.Substring(0,9);
			Console.WriteLine (temp);
		}

		public void Test4 () {
			string s = "就是就撒谎都用过啥V大亚会受到ashdghuasydygahsvdv加速度";
			string temp = s.Substring (1,15);
			Console.WriteLine (temp);
		}

		/// <summary>
		/// 字符串替换
		/// </summary>
		public void Test5 () {
			string s1 = "小明,你他妈个逼,草操艹肏你的妈耶!~";
//			string newstring = s1.Replace ("妈", "*");
//			Console.WriteLine (newstring);

			string pattern = @"[淫]|[妈逼]|[草操艹肏]";
			string newstr = Regex.Replace (s1, pattern, "*");
			Console.WriteLine (newstr);
		}

		/// <summary>
		/// 字符串插入(指定位置)
		/// </summary>
		public void Test6(){
			string s = "你好,中国";
			string newStr = s.Insert(0,"china");
			Console.WriteLine (newStr);
		}

		/// <summary>
		/// 判断字符串以什么什么结尾
		/// </summary>
		public void Test7(){
			string s = "你好,中国";
			bool b = s.EndsWith("中国");
			if (b) {
				Console.WriteLine ("是以中国结束");
			}
		}

		public void Test8 () {
			string s8 = "lm13142005@qq.com";
			bool b8 = s8.EndsWith ("@qq.com");



		}
		/// <summary>
		/// 字符串按照索引位置移除
		/// </summary>
		public void Test9(){
			string s = "你好,中国";
			string newStr = s.Remove(1,1);
			Console.WriteLine (newStr);
		}

		/// <summary>
		/// 字符串拼接
		/// </summary>
		public void Test10(){
			string s = "你好";
			s += "中国";
			Console.WriteLine (s);
		}

		/// <summary>
		/// 判断字符串是否相等
		/// </summary>
		public void Test11(){
			string s1 = "你好";
			string s2 = "你好1";
			string s3 = "你好2";
			//			if (s1 == s2) {
			//				Console.WriteLine ("相等的字符串");
			//			}

			if(string.Equals (s1, s2)){
				Console.WriteLine ("字符串s1和s2相等");
			}
		}

		/// <summary>
		/// 字符串转换值类型
		/// </summary>
		public void Test12(){
			string s = "123.";
			//			int i = int.Parse(s);

			int result = 0;
			bool l = int.TryParse (s, out result);
			if (l) {
				Console.WriteLine ("转换成功!~" + result);
			} else {
				throw new Exception ("传入数据包含非法字符,请处理!");
			}

		}

		public static void Main (string[] args) {
			
			new MainClass ().Test4 ();

			//练习1:判断字符串lm13142005@qq.com是否为合法邮箱
//						string email = "lm13142005qq.@com";
//						int indexAt = -1;
//						int indexDot = -1;
//						indexAt = email.IndexOf ("@");
//						indexDot = email.IndexOf (".");
//						if (indexAt != -1 && indexDot != -1) {
//							if (indexAt < indexDot) {
//								Console.WriteLine ("输入email!");
//							}
//						}
			 
			//匹配邮箱xxx@xxx.xxx
//						string email = "l@qqq.com";
//						string pattern = @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+";
//						if (Regex.IsMatch (email, pattern)) {
//							Console.WriteLine ("合法邮箱");
//						} else {
//							Console.WriteLine ("不合法邮箱");
//						}


			//作业:判断回文字符串121
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值