spf2011

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
  1. C#string关键字的映射实际上指向.NET基类System .String
  2. 使用string连接两个字符串实际上是创建了一个新字符串实例,把两部分内容复制到新字符串中。
  3. StringBuilder上可以进行的处理仅限于替换和添加或删除字符串中的文本,但它的工作方式非常高效。StringBuilder类基本上应在处理多个字符串时使用,如果只是连接两个字符串,使用System.String会比较好。
  4. 不能把StringBuilder转换为String。如果要把StringBuilder的内容输出为String,唯一的方式是使用ToString()方法。
  5. 正则表达式示例:

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Wrox.ProCSharp.RegularExpressionPlayaround
{
class MainEntryPoint
{
static void Main()
{
Find1();
Console.ReadLine();
}

static void Find1()
{
string text = @"XML has made a major impact in almost every aspect of software
development. Designed as an open, extensible, self-describing
language, it has become the standard for data and document delivery
on the web. The panoply of XML-related technologies continues to
develop at breakneck speed, to enable validation, navigation,
transformation, linking, querying, description, and messaging of data.";
string pattern = @"\bn\S*ion\b";
MatchCollection matches
= Regex.Matches(text, pattern, RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture);
WriteMatches(text, matches);
}

static void Find2()
{
string text = @"XML has made a major impact in almost every aspect of software
development. Designed as an open, extensible, self-describing
language, it has become the standard for data and document delivery
on the web. The panoply of XML-related technologies continues to
develop at breakneck speed, to enable validation, navigation,
transformation, linking, querying, description, and messaging of data.";
string pattern = @"\bn";
MatchCollection matches
= Regex.Matches(text, pattern, RegexOptions.IgnoreCase);
WriteMatches(text, matches);
}

static void WriteMatches(string text, MatchCollection matches)
{
Console.WriteLine(
"Original text was: \n\n" + text + "\n");
Console.WriteLine(
"No. of matches: " + matches.Count);
foreach (Match nextMatch in matches)
{
int Index = nextMatch.Index;
string result = nextMatch.ToString();
int charsBefore = (Index < 5) ? Index : 5;
int fromEnd = text.Length - Index - result.Length;
int charsAfter = (fromEnd < 5) ? fromEnd : 5;
int charsToDisplay = charsBefore + charsAfter + result.Length;
Console.WriteLine(
"Index: {0}, \tString: {1}, \t{2}", Index, result,
text.Substring(Index - charsBefore, charsToDisplay));
}
}
}
}
posted on 2011-03-31 21:50  spf2011  阅读(118)  评论(0)    收藏  举报