C# string专题

1、string.Format

Format(IFormatProvider, String, Object, Object, Object) 将字符串中的格式项替换为三个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息

格式化数值结果表

 

 

 Format(String, Object) 将字符串中的一个或多个格式项替换为指定对象的字符串表示形式

复制代码
1 DateTime birthdate = new DateTime(1993, 7, 28);
2 //{0}中的0表述格式项的索引,有几个格式项就有几个索引,数字从0开始。
3 string birth = string.Format("date is {0}", birthdate.ToLongDateString());
4 Console.Write(birth);
5 Console.ReadLine();
6 //  示例显示如下输出:
7 // date is 1993年7月28日
View Code
复制代码

Format(String, Object[]) 将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式

复制代码
 1 DateTime date1 = new DateTime(2009, 7, 1);
 2 TimeSpan hiTime = new TimeSpan(14, 17, 32);
 3 TimeSpan loTime = new TimeSpan(3, 16, 10);
 4 /**
 5              * {0:d}中的d表示格式项转化为十进制 
 6              * {1,11}中的11第二个对象的字符串表示形式在11个字符的字段中右对齐。 
 7              * (如果第一个对象的字符串表示形式的长度超过11个字符, 则将忽略首选字段宽度, 
 8              * 并将整个字符串插入到结果字符串中。)
 9              * */
10 string result1 = String.Format("Temperature on {0:d}:n{1,11}: {2} degrees (hi)n",
11                                            date1, hiTime,loTime);
12             Console.WriteLine(result1);
13             Console.WriteLine();
14             //若要在字段中左对齐字符串, 请在字段宽度前面加上负号, 如{0,-12}定义12个字符左对齐字段。
15             string result2 = String.Format("Temperature on {0:d}:n{1,-12}: {2} degrees (hi)n",
16                                            new object[] { date1, hiTime, loTime});
17             Console.WriteLine(result2);
18             //  示例显示如下输出:
19             //Temperature on 2009 / 7 / 1:
20             //14:17:32: 03:16:10 degrees(hi)
21             //Temperature on 2009 / 7 / 1:
22             //14:17:32    : 03:16:10 degrees(hi)
23             Console.ReadLine();
View Code
复制代码

Format(IFormatProvider, String, Object) 将指定字符串中的一个或多个格式项替换为对应对象的字符串表示形式。 参数提供区域性特定的格式设置信息

Format(IFormatProvider, String, Object[]) 将字符串中的格式项替换为指定数组中相应对象的字符串表示形式。 参数提供区域性特定的格式设置信息

复制代码
string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" };

DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0);
double value = 9164.32;

Console.WriteLine("Culture     Date                                Value\n");
foreach (string cultureName in cultureNames)
{
   System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
   string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", 
                                 culture.Name, dateToDisplay, value);
   Console.WriteLine(output);
}    
// 示例显示如下输出:
//    Culture     Date                                Value
//    
//    en-US       Tuesday, September 01, 2009         9,164.32
//    fr-FR       mardi 1 septembre 2009              9 164,32
//    de-DE       Dienstag, 1. September 2009         9.164,32
//    es-ES       martes, 01 de septiembre de 2009    9.164,32
View Code
复制代码

Format(String, Object, Object) 将字符串中的格式项替换为两个指定对象的字符串表示形式

复制代码
 1 Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 
 2 temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
 3 temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);
 4 
 5 Console.WriteLine("Temperature Information:\n");
 6 string output;   
 7 foreach (var item in temperatureInfo)
 8 {
 9    output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", 
10                           item.Key, item.Value);
11    Console.WriteLine(output);
12 }
13 // The example displays output like the following:
14 //       Temperature Information:
15 //       
16 //       Temperature at  2:00 PM on  6/1/2010:  87.5°F
17 //       Temperature at 10:00 AM on 12/1/2010:  36.8°F
View Code
复制代码

Format(IFormatProvider, String, Object, Object) 将字符串中的格式项替换为两个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息

Format(String, Object, Object, Object) 将字符串中的格式项替换为三个指定对象的字符串表示形式

复制代码
 1 string formatString = "    {0,10} ({0,8:X8})\n" + 
 2                       "And {1,10} ({1,8:X8})\n" + 
 3                       "  = {2,10} ({2,8:X8})";
 4 int value1 = 16932;
 5 int value2 = 15421;
 6 string result = String.Format(formatString, 
 7                               value1, value2, value1 & value2);
 8 Console.WriteLine(result);
 9 // The example displays the following output:
10 //                16932 (00004224)
11 //       And      15421 (00003C3D)
12 //         =         36 (00000024)
View Code
复制代码

 2、string.Compare(str1, str2, true)

返回值:

1 : str1大于str2

0 : str1等于str2

-1 : str1小于str2

3. 识别字符串是否为数字

C# 使用 Enumerable.All() 方法来识别字符串是否为数字

4. 字符串与字符数组转换

字符串转字符数组

5. 一次生成多个相同的字符 【2024-05-16 08:11:53

 

new string('A', 10) ;
string.Empty.PadRight(10, 'A');

 

posted @   船长华莱士  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示