深入C#
C#中的String类
他是专门处理字符串的(String),他在System的命名空间下,在C#中我们使用的是string
小写的string只是大写的String的一个别名(外号)使用大写和小写都是一样的
常用的字符串处理法
Java中常用的字符串处理方法?
1) IndexOf:字符串的检索
a) IndexOf(String s):重头开始检索
b) IndexOf(String s,startString s):从startString开始检索
2) 获取长度:.length()
3) 判断.equals(参数)
4) 得到字符串的子字符
subString(参数)
subString(参数1,参数2)
C#提供的字符串比较的方法
1) IndexOf:查找某个字符在字符串当中的位置
2) subString:从字符串中截取子字符
3) ToLower():转换成小写
4) ToUpper():大写
5) Time():去空格
6) Equals():比较字符串值
If(name == “”) 地址
If(name.Equals(String.Empty))值
“”和Empty的区别?
If(name == “”) 分配一个长度为空的存储空间
If(name.Equals(String.Empty)) 不会分配存储空间
判断空字符串的三种写法?这三个的性能比较?
Name.Length ==0 2
Name == String.Empty 1
Name == “” 3
7) joi():链接字符串
8) split():分割
获取邮箱用户名
需求:获取邮箱的用户名
兼用各种格式yes(YES)
循环执行
public void GetUserName()
{
string code;//用户选择
do{
this.PickNameFoemEmail();
Console.WriteLine("是否继续?yes
");
code = Console.ReadLine();
code = code.Trim().ToLower();
}while(code.Equals("yes"));
}
public void PickNameFoemEmail()
{
string emsil;// 获取邮箱
string name;//获取用户名
Console.WriteLine("请输入邮箱:");
emsil = Console.ReadLine();
Console.WriteLine("你的邮箱是{0}:",emsil);
// 提取
int posion = emsil.IndexOf("@");
if (posion > 0)
{
name = emsil.Substring(0, posion);
Console.WriteLine("你的邮箱地址是:{0}", name);
}
else
{
Console.WriteLine("你的邮箱格式错误");
}
}
Class1 c = new Class1();
c.GetUserName();
Console.ReadKey();
连接分割字符串
Join split
// 输入的字符串
string inputString;
// 分割后的字符串数组
string[] splitString;
// 连接后的
string joinString;
Console.WriteLine("请输入字符串,用空分开:");
inputString = Console.ReadLine();
splitString = inputString.Split(' ');
Console.WriteLine(@"\n分割后的:");
foreach (var item in splitString)
{
Console.WriteLine(item);
}
joinString = string.Join("+连接+",splitString);
Console.WriteLine("\n连接后的字符串:{0}",joinString);
@”\n转义符”:忽略掉
Format格式化(不是清除的意思)
String name = “Tom”;
Console.WritrLine(“我的名字:{0},我的年龄{1}”,name,22);
{x}占位符的方式去输出
string name;
string birthday;
int height;
string bloodType;
string planet;
string loveFood;
string record;
Console.WriteLine("欢迎来到“C#”的世界!");
Console.WriteLine("请输入你的个人信息,我将为你建立个人档案:");
Console.Write("姓名:");
name = Console.ReadLine();
Console.Write("出生年月:(*年*月*日):");
birthday = Console.ReadLine();
Console.Write("身高(cm):");
height = int.Parse(Console.ReadLine());
Console.Write("星座:");
planet = Console.ReadLine();
Console.Write("血型:");
bloodType = Console.ReadLine();
Console.Write("喜欢的食物:");
loveFood = Console.ReadLine();
record = string.Format("姓名:{0}\n出生年月:{1}\n身高:{2}\n星座:{3}\n血型:{4}\n喜欢的食物:{5}",name,birthday,height,bloodType,planet,loveFood);
Console.WriteLine("\n这是你的个人档案:");
Console.WriteLine(record);
Console.ReadKey();
Grammar:String myString = string.Format(“格式化字符串”,参数列表)
2 * 3 = 6
String myString = string.Format(“{0}乘以{1}等于{2}”,2,3 ,2 * 3);
如输出货币
语法:
格式字符串包括:固定文本和格式项
格式项
Console.WriteLine("{0}",50);
Console.WriteLine(String.Format("{0,-8:F2}",50));
Console.WriteLine(String.Format("{0,8:C2}", 50));
Format()方法的格式化字符串中各种格式化定义符和示例
1) C:货币格式
2) D十进制格式
3) F小数点后固定位数
4) 用逗号隔开的数字
5) 百分比计数法
6) 十六进制格式
Console.WriteLine("{0}",String.Format("{0:C3}",3000));
Console.WriteLine("{0}",String.Format("{0:D3}",2000));
Console.WriteLine("{0}", String.Format("{0:F3}", 2000));
随机推荐
- 知识点---<input>、<textarea>
一.在pc端的input是一个大的知识点 [1]文本框 <input type="text"> [2] 密码框 <input type="passwor ...
- file常用功能
构造方法 File(String pathname):将指定的路径名转换成一个File对象 File f = new File("D:\\a\\b.txt"); File(Stri ...
- java 下载网络文件
1.FileUtils.copyURLToFile实现: import java.io.File; import java.net.URL; import org.apache.commons.io. ...
- 关于pycharm有时候提取不了form表单POST提交的数据
1.有可能标签没有name属性 2.name属性要放在第一个位置,放在末尾有时候会出现BUG导致识别不出,提取的值为None.
- 100-days: twenty-eight
Title: Lawrence Ferlinghetti's(劳伦斯·费林盖蒂) enduring San Francisco(旧金山) 劳伦斯·费林盖蒂心中的旧金山,历久弥新 费林盖蒂:美国垮掉的一 ...
- mysql学习5:数据库设计
mysql学习5:数据库设计 本文转载:https://blog.51cto.com/9291927/2087925:原创为天山老妖S 一.数据库设计简介 按照规范设计,将数据库的设计过程分为六个阶段 ...
- requests+django+bs4实现一个web微信的功能
前言: 今天我们利用requests模块+django+bs4浏览器来实现一个web微信的基本功能,主要实现的功能如下 a.实现返回二维码 b.实现手机扫码后二维码变成变成头像 c.实现手机点击登陆成 ...
- jdk和tomcat的安装
https://blog.csdn.net/angel_w/article/details/78580528
- 阅读:JAVA 3& 4
随机数: Random rand = new Random(47); // 产生随机算子.47 is seed. for incovating predicatable random numberin ...
- MAVEN工程相关配置
MAVEN工程插件安装: Name: MavenArchiver Location: https://repo1.maven.org/maven2/.m2e/connectors/m2eclipse- ...