Arogl Darthu's Blog

profound astoundment

  • Homepage

Apr 6: Truncating text while respecting word boundaries.

Every so once in a while the issue rises where you need to truncate a piece of text to a certain maximum length.
Now that in itself is not so hard to do. But the result can look quite strange when the truncation point happens in the middle of a word.
As always StackOverflow provides a good starting point.
I just thought using a Regular Expression would be more concise. So here is my solution:


private const string Ellipsis = "...";

// Match word boundaries at the end of a word.
private static Regex wordRegex = new Regex(@"(?<=\w)(?!\w)", RegexOptions.Compiled);
       
private static string Truncate(string source, int length)
{
    int maxLength = length - Ellipsis.Length;
    if (source != null && source.Length > length)
    {
        int afterLastWordIndex = 0;
        foreach (Match m in wordRegex.Matches(source))
        {
            if (m.Index <= maxLength)
            {
                afterLastWordIndex = m.Index;
            }
            else
            {
                break;
            }
        }

        return String.Concat(source.Substring(0, afterLastWordIndex), Ellipsis);
    }

    return source;
}

static void Main(string[] args)
{
    string source =   "The quick brown fox jumps over the lazy dog";
    Console.WriteLine("         1         2         3         4         5");
    Console.WriteLine("12345678901234567890123456789012345678901234567890");
    Console.WriteLine(source);
    int[] truncingLengths = { 0, 1, 3, 4, 8, 9, 10, 15, 19, 20, 21, 22, 23, 28, 30, 33, 42, 43, 50 };

    foreach (int truncAt in truncingLengths)
    {
        Console.WriteLine("{0}: \"{1}\"", truncAt, Truncate(source, truncAt));
    }

    Console.ReadLine();
}
 

Posted by Twan Jacobs in C# Comments: (0) Trackbacks: (0)

Jun 1: MSBuild Exec task - IgnoreStandardErrorWarningFormat

If you are using MSBuild 4.0.30319 and try to set the property IgnoreStandardErrorWarningFormat on the Exec task you might run into the following message error:
error MSB4064: The "IgnoreStandardErrorWarningFormat" parameter is not supported by the "Exec" task.

The solution is very, very simple: be sure to add ToolsVersion="4.0" to the Project tag.

<Project DefaultTargets="Bla" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Target Name="Bla">
    <Exec IgnoreStandardErrorWarningFormat="true" Command="dir /w" />
  </Target>
</Project>
 

Posted by Twan Jacobs in WTF Comments: (0) Trackbacks: (0)

May 9: Upgrade test project from VS2005 to VS2010

The solution contains several projects, including test projects. All of which now target .Net Framework 2.0 (although some already reference .Net Framework 3.0 or 3.5 assemblies). We do not want to upgrade to .Net Framework 4.0 just yet. Goal of the upgrade is to have every project in the solution target .Net Framework 3.5.

After converting the solution to VS2010 most of the projects reference .Net Framework 2.0. Some of which do not build, because of references to assemblies targeted at a higher .Net Framework. Visual Studio 2010 does not allow cross-referencing to higher .Net Framework assemblies. Fixing this problem is quite simple: just re-target the project to .Net Framework 3.5.

All of the test projects have been set to .Net Framework 4.0. One could discuss if this is acceptable for test projects as these will not be released. But for now it was decided to keep every project targeted at .Net Framework 3.5. Fortunately most of the test projects were easily re-targeted to .Net Framework 3.5. Hint: if you run into problems here, make sure VS2010 SP1Rel is installed.

For one of the test projects the solution just would not load it if re-targeted at .Net Framework 3.5. Each time the project was re-targeted, the conversion wizard would just pop up and set it back to .Net Framework 4.0. Eventually we found a workaround by editing the project file by hand after the conversion.

Two manual fixes were needed:
  • Change reference of "Microsoft.QualityTools.UnitTestFramework" to
    <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
  • Remove contents of the tag <FileUpgradeFlags>0</FileUpgradeFlags>, which hints VS2010 to start the conversion wizard.
Posted by Twan Jacobs in WTF Comments: (0) Trackbacks: (0)
« previous page   (Page 1 of 5, totaling 14 entries)   next page »

Calendar

Back May '12
Mo Tu We Th Fr Sa Su
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Archives

  • May 2012
  • April 2012
  • March 2012
  • Recent...
  • Older...

Categories

  • XML BizTalk
  • XML C#
  • XML Daily Didst
  • XML Infra
  • XML SQL
  • XML WCF
  • XML WTF


All categories

Syndicate This Blog

  • XML RSS 2.0 feed
  • XML RSS 2.0 Comments

Blog Administration

Open login screen

Powered by

Serendipity PHP Weblog
 

Layout by Andreas Viklund | Serendipity template by Carl