本帖内容已移至:各种计算机语言简介和总结 – C#学习心得
1.C#中的正则表达式处理,对应的是Regex类
C#中的Regex类处理正则表达式。
1.1正则表达式中包含双引号,是用两个双引号去表示
其中如果包含的字符串中包含双引号,那么就两个双引号表示,而不是反斜杠加上双引号(\”),也不是斜杠加上双引号(/”)。示例代码:
1 2 | string pat = @"var\s+primedResponse=\{""items""\:\[\{(.*)}\]\};\s+\$Do\.register\(""primedResponse""\);" ; Regex rx = new Regex(pat,RegexOptions.Compiled); |
2.C#中的TreeView控件
2.1 添加Node的方法:
当前已有一个TreeView控件trvFolder,添加节点的代码如下:
1 2 | TreeNode curNode = new TreeNode(name); trvFolder.Nodes.Add(curNode); |
2.2给TreeView添加图标
先拖拽出来一个ImageList控件iglIcons,然后手动编辑ImageList的Images属性Collection,添加几个小图片。
对应的index分别是0,1,2,。。。,然后用:
1 | trvFolder.ImageList = iglIcons; |
将其关联到当前TreeView控件trvFolder。
最后,每次添加TreeNode的时候,多加一句:
1 | curNode.ImageIndex = 0; |
即为:
1 2 3 | TreeNode curNode = new TreeNode(name); curNode.ImageIndex = 0; trvFolder.Nodes.Add(curNode); |
其中0为图片的index,可根据自己的实际情况改为对应的index。
这样就可以实现给不同的TreeNode添加对应的图标了。
3.C#中的字典类型变量
C#中也有类似于Python中的字典类型的变量
参考代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Dictionary<String, int > dicPicIdx; Dictionary< string , int >.KeyCollection dicKeys; dicPicIdx = new Dictionary< string , int >(); dicPicIdx.Add( "EmptyDocumentFolder" , 1); dicPicIdx.Add( "NonEmptyDocumentFolder" , 1); dicPicIdx.Add( "NonEmptyAlbum" , 3); dicPicIdx.Add( "EmptyAlbum" , 3); dicPicIdx.Add( "NonEmptyFavoriteFolder" , 4); dicPicIdx.Add( "EmptyFavoriteFolder" , 4); dicPicIdx.Add( "Photo" , 6); dicPicIdx.Add( "Audio" , 7); dicPicIdx.Add( "Video" , 8); dicKeys = dicPicIdx.Keys; |
4.C#中类似于spritf的函数
C#中,类似于C中常用的spritf函数,是String.Format函数。
最简单的用法举例如下:
1 2 | string spritfTestStr = String.Format( "Test sprintf in C#, number={0:D}, string=\"{1:s}\", float={2:0.000}" , 100, "crifan" , Math.PI); //spritfTestStr = Test sprintf in C#, number=100, string="crifan", float=3.142 |
关于Format函数的更多的示例,可以参考微软官方文档:
String.Format Method (String, Object)
关于其他更多不同类型的参数,比如日期,数值,枚举等,如何指定对应的格式,可以参考:
- For more information about the composite formatting feature supported by methods such as Format, AppendFormat, and some overloads of WriteLine, see Composite Formatting.
- For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
- For more information about date and time format specifiers, see Standard DateTime Format Strings and Custom DateTime Format Strings.
- For more information about enumeration format specifiers, see Enumeration Format Strings.
- For more information about formatting, see Formatting Types and Formatting Overview.
5.关于UrlEncode把空格编码为加号“+”而不是%20的问题
默认的HttpUtility.UrlEncode会把空格编码为加号,而不是很多人所期望的%20,导致很多人以为函数出了问题呢。其实,微软官方文档HttpUtility.UrlEncode 方法中,已经解释了此问题:
您可以使用 UrlEncode 方法或 UrlPathEncode 方法对 URL 编码。但是,方法返回不同的结果。UrlEncode 方法将每个空格字符转换为加号 (+) 字符。UrlPathEncode 方法将每个空格字符转换为字符串 “%20″,它表示一个用十六进制表示法表示的空格。在对 URL 的路径部分编码时使用 UrlPathEncode 方法,以保证一致的已解码 URL,与执行解码的平台或浏览器无关。
所以,想要把空格编码为%20,用UrlPathEncode即可。
【参考资料】
正则表达式包含引号怎么办?
http://tech.techweb.com.cn/thread-236996-1-1.html
c# Regex常用
http://www.cnblogs.com/jorgen/archive/2010/10/14/1851555.html
c#学习笔记《1》——regex类(个人理解)
http://hi.baidu.com/wenyouming1989/blog/item/c0d1f95a1e296cd39c8204b4.html
为 Windows 窗体 TreeView 控件设置图标
http://msdn.microsoft.com/zh-cn/library/aa983725
winform中利用Treeview模仿资源管理器实现图片文件列表
http://blog.csdn.net/mngzilin/article/details/5535136
C#键值对容器
http://lujian811.blog.163.com/blog/static/10190113201071933020824/
转载请注明:在路上 » 【记录】C#学习心得