CSharpFeeds - All your C# feeds in one place.

Sponsors

Monday, December 18, 2006

CS Dev Guide: TextParts

by Keyvan Nayyeri via Keyvan Nayyeri on 12/18/2006 4:09:40 PM

As I described before, TextParts are a special type of TextTokens in blog application.  You can use them to create some text shortcuts to replace them with a longer text when post is being rendered.  In Community Server 2.1 TextParts are known as Snippets.

Overview

Before talking about development details it's worth to have a background about TextParts structure.  Each TextPart consists of several colon delimited values that build it.  For example, suppose this TextPart: [Keyvan:Nayyeri:1984].  It consists of three separate values: Keyvan, Nayyeri and 1984.

In Community Server APIs two classes and one namespace provide appropriate tools to work with TextParts:

  • CommunityServer.Components.TextPart: Main class which represents a TextPart in APIs.
  • CommunityServer.Components.TextPartSet: A class to keep a list of TextParts.  It's simply a collection of TextParts.
  • CommunityServer.Components.TextPartManager: A namespace which provides two static methods to work with TextParts.

Now it's time to see some details about above classes and namespace.

TextPart

TextPart class has four properties:

  • Count: Integer value of the number of elements in a TextPart (3 in my example).
  • Name: String value of first element in a TextPart ("Keyvan" in my example).
  • Part: String value of whole TextPart ("[Keyvan:Nayyeri:1984]" in my example).
  • Value: String value of second element in TextPart ("Nayyeri" in my example).

This class also has three main methods:

  • ConvertToBoolean(): Gets the index of an element in TextPart and converts it to Boolean type.
  • ConvertToInt(): Gets the index of an element in TextPart and converts it to integer type.
  • Replace(): Gets two parameters.  One of them is the string value of old text which contains that TextPart and second value is the text value that should be replaced with whole TextPart.  It returns a new string value which contains the replaced text.

Note that you can refer to a specific element in a TextPart using an index because it represent elements like an array.  For example by using TextPart[1] in my example, you can get "Nayyeri" value.  On the other hand you can use Get() method to do this.

TextPartSet

TextPartSet is another class which works as a collection for TextPart objects.  You'll see its usage in next section when I describe static methods in TextPartManager but for now I'd like to say you can retrieve an instance of TextPart object by referring to an index or by calling Get() method in this class.  You can also iterate through all TextPart objects in it.

TextPartManager

This namespace is where you can find two useful static methods:

  • CreatePart(): You pass the string value of a part and it returns an instance of TextPart object.
  • Parse(): It gets a text, parses it, and returns a TextPartSet object which contains all available TextParts in that text.

Sample

Let's discover a sample.  In below code, I write a function which parses current post and finds all TextParts in its body then replaces all TextParts with "Here was a TextPart" text.  It finally returns a new value for post body.

String GetFormattedBody()

{

    CSContext context = CSContext.Current;

 

    String body = context.Post.Body;

    TextPartSet textParts = TextPartManager.Parse(body);

    foreach (TextPart textPart in textParts)

    {

        body = textPart.Replace(body, "Here was a TextPart");

    }

    TextPart part = new TextPart();

    return body;

}

Now playing: Fandango Nights - Willie And Lobo

email it!bookmark it!digg it!

Original Post: CS Dev Guide: TextParts

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us