format item of C# String.Format

The format item
A format item has this syntax:

{index[ ,alignment][ :formatString] }
Brackets denote optional elements. The opening and closing braces are required. (To include a literal opening or closing brace in the format string, see the “Escaping Braces” section in the Composite Formatting article.)

For example, a format item to format a currency value might appears like this:

String.Format(“{0,-10:C}”, 126347.89m);
String.Format(“{0,-10:C}”, 126347.89m);
String::Format(“{0,-10:C}”, (Decimal) 126347.89);
String::Format(“{0,-10:C}”, (Decimal) 126347.89);
String.Format(“{0,-10:C}”, 126347.89d)
String.Format(“{0,-10:C}”, 126347.89d)

A format item has the following elements:

index
The zero-based index of the argument whose string representation is to be included at this position in the string. If this argument is null, an empty string will be included at this position in the string.

alignment
Optional. A signed integer that indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). If you omit alignment, the string representation of the corresponding argument is inserted in a field with no leading or trailing spaces.

formatString
Optional. A string that specifies the format of the corresponding argument’s result string. If you omit formatString, the corresponding argument’s parameterless ToString method is called to produce its string representation. If you specify formatString, the argument referenced by the format item must implement the IFormattable interface. Types that support format strings include:

All integral and floating-point types. (See Standard Numeric Format Strings and Custom Numeric Format Strings.)

DateTime and DateTimeOffset. (See Standard Date and Time Format Strings and Custom Date and Time Format Strings.)

All enumeration types. (See Enumeration Format Strings.)

TimeSpan values. (See Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.)

GUIDs. (See the Guid.ToString(String) method.)

However, note that any custom type can implement IFormattable or extend an existing type’s IFormattable implementation.

关于ESCAPE 特殊字符, MySQL 和 SQL Server 的一个区别

MySQL 默认使用’\’作为转义符, 但SQL Server不用, SQL Server 有自己的一套方法:
1. 将其放到[]内:
SELECT * FROM table WHERE f LIKE N’%[%]%’
SELECT * FROM table WHERE f LIKE N’%[_]%’
那如果要查询'[‘本身, 怎么办呢?
SELECT * FROM table WHERE f LIKE N’%[[]%’
即可.
那要查’]’呢? 直接使用即可,因为很显然这里不会造成歧义:
SELECT * FROM table WHERE f LIKE N’%]%’
查询既包含'[‘又包含’]’,该如何呢?例如要查询包含”[abc]”的记录, 很显然’%[abc]%’不行!
据说双重[[]]即可!(未用过,不知道到底对不对)
SELECT * FROM table WHERE f LIKE N’%[[abc]]%’
2. 使用 ESCAPE 关键字:
总会有用不到的字符吧, 例如我经常这样用:
SELECT * FROM table WHERE f LIKE N’%[abc]%’ ESCAPE ‘'
前提是该字段从不使用字符'
‘, 如果这条不能保证, 总能找到一个不用的吧. 全用到了? 那另想它法吧. 我还从未遇到过呐,遇到时再去想办法吧.
3. MySQL 由于可以使用’\’作为转义符, 因此可查询回车换行等字符:
SELECT * FROM MySqlTable WHERE f LIKE ‘%\r\n%’
SQL SERVER 傻眼了, 怎么办呢?
SELECT * FROM SqlServerTable WHERE f LIKE ‘%’+NCHAR(0x0D)+NCHAR(0x0A)+’%’
好麻烦啊. 不知道有没有更好的方法, 求教.

汉字简化合二为一的字

简化字 正体字(繁体字)
么 幺麼麽
伪 偽僞
冲 沖衝
历 曆歷
发 發髮
台 檯臺颱
坛 壇罈
复 復複覆
尽 儘盡
干 乾幹
并 並併
当 噹當
汇 匯彙
硷 礆鹼
系 係繫
胡 衚鬍
脏 臟髒
荡 盪蕩
获 獲穫
采 埰採
钟 鍾鐘
须 須鬚

由于有这些字的捣乱,转换麻烦.繁转简比较容易,但简转繁就麻烦了.见到一个发,你说转成哪个發髮?不得不根据上下文来判断,嗯,麻烦.
不过幸好这个表并不算大(22个字).

SQL Server 的一个陷阱

今天发现一些数据有异常,开头的字符是Unicode 0xFEFF, 于是将其删除,于是执行下列语句:
UPDATE table SET data = RIGHT(data, LEN(data)-1) WHERE LEN(data)>0 AND data LIKE NCHAR(0xFEFE)+N’%’
结果很不幸啊,全部数据都被去掉了一个字符
于是,又用下列语句验证了一下:
SELECT COUNT() FROM table WHERE 1=1
SELECT COUNT(
) FROM table WHERE data LIKE NCHAR(0xFEFE)+N’%’
结果一样,就是说,全部数据都中选.

幸好事先保存了备份. 不然麻烦可真大.