当前位置: 首页>>代码示例>>C#>>正文


C# Format.Split方法代码示例

本文整理汇总了C#中Format.Split方法的典型用法代码示例。如果您正苦于以下问题:C# Format.Split方法的具体用法?C# Format.Split怎么用?C# Format.Split使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Format的用法示例。


在下文中一共展示了Format.Split方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EvaluateFormat

        public void EvaluateFormat(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            // Ignore formats that start with "?" (this can be used to bypass this extension)
            if (format == null || format.baseString[format.startIndex] == ':')
            {
                return;
            }

            // Extract the plural words from the format string:
            var pluralWords = format.Split("|");
            // This extension requires at least two plural words:
            if (pluralWords.Count == 1) return; 

            // See if the value is a number:
            var currentIsNumber =
                current is byte || current is short || current is int || current is long
                || current is float || current is double || current is decimal;
            // This extension only formats numbers:
            if (!currentIsNumber) return; 

            // Normalize the number to decimal:
            var value = Convert.ToDecimal(current);

            // Get the plural rule:
            var provider = formatDetails.Provider;
            var pluralRule = GetPluralRule(provider);

            if (pluralRule == null)
            {
                // Not a supported language.
                return;
            }

            var pluralCount = pluralWords.Count;
            var pluralIndex = pluralRule(value, pluralCount);

            if (pluralIndex < 0 || pluralWords.Count <= pluralIndex)
            {
                // The plural rule should always return a value in-range!
                throw new FormatException(format, "Invalid number of plural parameters", pluralWords.Last().endIndex);
            }

            // Output the selected word (allowing for nested formats):
            var pluralForm = pluralWords[pluralIndex];
            formatDetails.Formatter.Format(output, pluralForm, current, formatDetails);
            handled = true;
        }
开发者ID:Avatarchik,项目名称:AnimatorAccess,代码行数:47,代码来源:PluralLocalizationFormatter.cs

示例2: EvaluateFormat

        /// <summary>Takes the current object and writes it to the output, using the specified format.</summary>
        /// <param name="current">The object to be formatted.</param>
        /// <param name="format">Represents a parsed format string.</param>
        /// <param name="handled">Set to indicate whether the formatter has formatted the object.</param>
        /// <param name="output">The output to write to.</param>
        /// <param name="formatDetails">Contains extra information about the item being formatted.</param>
        public void EvaluateFormat(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            // validate
            if (!current.IsNumber() || format == null)
                return;
            if (format.baseString[format.startIndex] == ':')
                return; // ':' is a magic prefix to bypass pluralisation

            // parse format
            decimal count = Convert.ToDecimal(current);
            var pluralWords = format.Split("|"); // extract plural words
            int pluralCount = pluralWords.Count;
            if (pluralCount == 1)
                return;

            // get pluralisation rules
            CultureInfo culture = (formatDetails.Provider as CultureInfo) ?? this.DefaultCulture ?? CultureInfo.CurrentUICulture;
            PluralRuleset ruleset = this.Rulesets.GetRuleset(culture.TwoLetterISOLanguageName);
            if (ruleset == null)
                return; // unknown language
            if ((pluralCount < 0 || pluralCount > ruleset.Forms) && !ruleset.AlternativeFormSelectors.ContainsKey(pluralCount))
            {
                // determine the accepted number of forms
                string expected = String.Join(
                    ", ",
                    new[] { ruleset.Forms }
                        .Union(ruleset.AlternativeFormSelectors.Keys)
                        .Select(p => p.ToString())
                        .ToArray()
                );
                throw new FormatException(format, String.Format("There are an invalid number of plural parameters (got {0}, expected one of: {1}).", pluralCount, expected), pluralWords.Last().endIndex);
            }

            // get plural form
            int pluralIndex = ruleset.SelectForm(count, pluralCount);
            if (pluralIndex < 0 || pluralIndex > pluralCount - 1)
                throw new FormatException(format, String.Format("The ruleset unexpectedly returned an invalid plural form (expected 0-{0}, got {1}).", pluralCount - 1, pluralIndex), pluralWords.Last().endIndex);

            // output the selected word (allowing for nested formats):
            var pluralForm = pluralWords[pluralIndex];
            formatDetails.Formatter.Format(output, pluralForm, current, formatDetails);
            handled = true;
        }
开发者ID:vhenzl,项目名称:SmartFormat,代码行数:49,代码来源:PluralFormatter.cs

示例3: EvaluateFormat

        /// <summary>
        /// If the source value is an array (or supports ICollection), 
        /// then each item will be custom formatted.
        /// 
        /// 
        /// Syntax: 
        /// #1: "format|spacer"
        /// #2: "format|spacer|last spacer"
        /// #3: "format|spacer|last spacer|two spacer"
        /// 
        /// The format will be used for each item in the collection, the spacer will be between all items, and the last spacer will replace the spacer for the last item only.
        /// 
        /// Example:
        /// CustomFormat("{Dates:D|; |; and }", {#1/1/2000#, #12/31/2999#, #9/9/9999#}) = "January 1, 2000; December 31, 2999; and September 9, 9999"
        /// In this example, format = "D", spacer = "; ", and last spacer = "; and "
        /// 
        /// 
        /// 
        /// Advanced:
        /// Composite Formatting is allowed in the format by using nested braces.
        /// If a nested item is detected, Composite formatting will be used.
        ///
        /// Example:
        /// CustomFormat("{Sizes:{Width}x{Height}|, }", {new Size(4,3), new Size(16,9)}) = "4x3, 16x9"
        /// In this example, format = "{Width}x{Height}".  Notice the nested braces.
        /// 
        /// </summary>
        public void EvaluateFormat(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            // This method needs the Highest priority so that it comes before the PluralLocalizationExtension and ConditionalExtension

            // This extension requires at least IEnumerable
            var enumerable = current as IEnumerable;
            if (enumerable == null) return;
            // Ignore Strings, because they're IEnumerable.
            // This issue might actually need a solution
            // for other objects that are IEnumerable.
            if (current is string) return;
            // If the object is IFormattable, ignore it
            if (current is IFormattable) return;

            // This extension requires a | to specify the spacer:
            if (format == null) return;
            var parameters = format.Split("|", 4);
            if (parameters.Count < 2) return;

            // Grab all formatting options:
            // They must be in one of these formats:
            // itemFormat|spacer
            // itemFormat|spacer|lastSpacer
            // itemFormat|spacer|lastSpacer|twoSpacer
            var itemFormat = parameters[0];
            var spacer = (parameters.Count >= 2) ? parameters[1].Text : "";
            var lastSpacer = (parameters.Count >= 3) ? parameters[2].Text : spacer;
            var twoSpacer = (parameters.Count >= 4) ? parameters[3].Text : lastSpacer;

            if (!itemFormat.HasNested)
            {
                // The format is not nested,
                // so we will treat it as an itemFormat:
                var newItemFormat = new Format(itemFormat.baseString);
                newItemFormat.startIndex = itemFormat.startIndex;
                newItemFormat.endIndex = itemFormat.endIndex;
                newItemFormat.HasNested = true;
                var newPlaceholder = new Placeholder(newItemFormat, itemFormat.startIndex, formatDetails.Placeholder.NestedDepth);
                newPlaceholder.Format = itemFormat;
                newPlaceholder.endIndex = itemFormat.endIndex;
                newItemFormat.Items.Add(newPlaceholder);
                itemFormat = newItemFormat;
            }

            // Let's buffer all items from the enumerable (to ensure the Count without double-enumeration):
            ICollection items = current as ICollection;
            if (items == null)
            {
                var allItems = new List<object>();
                foreach (var item in enumerable)
                {
                    allItems.Add(item);
                }
                items = allItems;
            }

            int oldCollectionIndex = CollectionIndex; // In case we have nested arrays, we might need to restore the CollectionIndex
            CollectionIndex = -1;
            foreach (object item in items) {
                CollectionIndex += 1; // Keep track of the index

                // Determine which spacer to write:
                if (spacer == null || CollectionIndex == 0)
                {
                    // Don't write the spacer.
                }
                else if (CollectionIndex < items.Count - 1) {
                    output.Write(spacer, formatDetails);
                }
                else if (CollectionIndex == 1)
                {
                    output.Write(twoSpacer, formatDetails);
                }
//.........这里部分代码省略.........
开发者ID:kukawski,项目名称:SmartFormat,代码行数:101,代码来源:ListFormatter.cs

示例4: EvaluateFormat

        public void EvaluateFormat(object current, Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            if (format == null) return;
            // Ignore a leading ":", which is used to bypass the PluralLocalizationExtension
            if (format.baseString[format.startIndex] == ':')
            {
                format = format.Substring(1);
            }

            // See if the format string contains un-nested "|":
            var parameters = format.Split("|");
            if (parameters.Count == 1) return; // There are no parameters found.

            // See if the value is a number:
            var currentIsNumber = 
                current is byte || current is short || current is int || current is long
                || current is float || current is double || current is decimal;
            // An Enum is a number too:
            if (currentIsNumber == false && current != null && current.GetType().IsEnum)
            {
                currentIsNumber = true;
            }
            var currentNumber = currentIsNumber ? Convert.ToDecimal(current) : 0;


            int paramIndex; // Determines which parameter to use for output

            // First, we'll see if we are using "complex conditions":
            if (currentIsNumber) {
                paramIndex = -1;
                while (true)
                {
                    paramIndex++;
                    if (paramIndex == parameters.Count)
                    {
                        // We reached the end of our parameters,
                        // so we output nothing
                        handled = true;
                        return;
                    }
                    bool conditionWasTrue;
                    Format outputItem;
                    if (!TryEvaluateCondition(parameters[paramIndex], currentNumber, out conditionWasTrue, out outputItem))
                    {
                        // This parameter doesn't have a
                        // complex condition (making it a "else" condition)

                        // Only do "complex conditions" if the first item IS a "complex condition".
                        if (paramIndex == 0)
                        {
                            break;
                        }
                        // Otherwise, output the "else" section:
                        conditionWasTrue = true;
                    }

                    // If the conditional statement was true, then we can break.
                    if (conditionWasTrue)
                    {
                        formatDetails.Formatter.Format(output, outputItem, current, formatDetails);
                        handled = true;
                        return;
                    } 
                }
                // We don't have any "complex conditions", 
                // so let's do the normal conditional formatting:
            }


            var paramCount = parameters.Count;

            // Determine the Current item's Type:
            if (currentIsNumber) {
                if (currentNumber < 0)
                {
                    paramIndex = paramCount - 1;
                } 
                else
                {
                    paramIndex = Math.Min((int)Math.Floor(currentNumber), paramCount - 1);
                }
            } 
            else if (current is bool) {
                // Bool: True|False
                bool arg = (bool)current;
                if (arg)
                {
                    paramIndex = 0;
                }
                else
                {
                    paramIndex = 1;
                }
            } 
            else if (current is DateTime) {
                // Date: Past|Present|Future   or   Past/Present|Future
                System.DateTime arg = (DateTime)current;
                if (paramCount == 3 && arg.Date == DateTime.Today) 
                {
                    paramIndex = 1;
//.........这里部分代码省略.........
开发者ID:Avatarchik,项目名称:AnimatorAccess,代码行数:101,代码来源:ConditionalFormatter.cs


注:本文中的Format.Split方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。