Argentini.Halide.H3Text.Crop C# (CSharp) Метод

Crop() публичный статический Метод

Return the current string cropped by number of words or characters.

Words: returns the first "xNumber" of words in the string, including any intermediate punctuation, etc. Trailing punctuation is always removed.

Characters: returns the first "xNumber" of characters in the string, including any intermediate punctuation, etc. When it locates the last character, it scans ahead to make sure it's not breaking a word. If it is in the middle of a word, it will skip ahead to the next non-alpha-numeric chatacter and break on that, so your cropped string may be slightly larger than the number of characters you requested. Trailing punctuation is always removed.

public static Crop ( string strVar, int xNumber, CropType cropType, string xChars, string endWhenCropped ) : String
strVar string String to process.
xNumber int Number of words or characters (as close as possible) to return.
cropType CropType Enum value: CropType.Characters or CropType.Words.
xChars string Valid characters on which to break (defaults to space characters).
endWhenCropped string String to append to the return value, only if the string had to be cropped.
Результат String
        public static String Crop(string strVar, int xNumber, CropType cropType, string xChars, string endWhenCropped)
        {
            string outt = strVar;

            if (xNumber > 0)
            {
                switch (cropType)
                {
                    case CropType.Words:

                        Regex x = new Regex(@"((\w*?)(\W|\z)){0," + xNumber + @"}",	RegexOptions.Singleline);
                        MatchCollection mc = x.Matches(strVar);
                        outt = mc[0].ToString();

                        break;

                    case CropType.Characters:

                        string breakOn = xChars;

                        if (String.IsNullOrEmpty(breakOn))
                        {
                            breakOn = " ";
                        }

                        int test = 0;
                        int index = outt.Length;

                        if (strVar.Length > xNumber)
                        {
                            for (int X = 0; X < breakOn.Length; X++)
                            {
                                test = outt.IndexOf(breakOn.Substring(X, 1), xNumber);

                                if (test < index && test >= xNumber )
                                {
                                    index = test;
                                }
                            }

                            if (index < 1 || index >= outt.Length)
                            {
                                index = xNumber;
                            }

                            else
                            {
                                index++;
                            }

                            outt = strVar.Substring(0, index);
                        }

                        break;
                }

                if (outt.Length < strVar.Length)
                {
                    while (outt.Length > 0 && !H3Identify.HasPattern(H3Identify.REGEX_IS_ALPHA_NUMBER_ONLY, outt.Substring(outt.Length-1, 1)))
                    {
                        outt = outt.Substring(0, outt.Length - 1);
                    }

                    outt += endWhenCropped;
                }
            }

            return outt;
        }

Same methods

H3Text::Crop ( string strVar, int xNumber, CropType cropType ) : String
H3Text::Crop ( string strVar, int xNumber, CropType cropType, string xChars ) : String