/*
* 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
}
}
}
字符串 - string
最新推荐文章于 2021-03-26 09:39:59 发布