栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么要使用String.Format?[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么要使用String.Format?[重复]

我可以看到许多原因:

可读性

string s = string.Format("Hey, {0} it is the {1}st day of {2}.  I feel {3}!", _name, _day, _month, _feeling);

vs:

string s = "Hey," + _name + " it is the " + _day + "st day of " + _month + ".  I feel " + feeling + "!";

格式说明符 (这包括您可以编写自定义格式符的事实)

string s = string.Format("Invoice number: {0:0000}", _invoiceNum);

vs:

string s = "Invoice Number = " + ("0000" + _invoiceNum).Substr(..... )

字符串模板的持久性

如果我要将字符串模板存储在数据库中怎么办?使用字符串格式:

_id         _translation  1         Welcome {0} to {1}.  Today is {2}.  2         You have {0} products in your basket.  3         Thank-you for your order.  Your {0} will arrive in {1} working days.

vs:

_id         _translation  1         Welcome  2         to  3         .  Today is  4         .   5         You have  6         products in your basket.  7         Someone  8         just shoot  9         the developer.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442693.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号