Split String into smaller Strings by length variable

前端 未结 12 1936
谎友^
谎友^ 2020-12-01 07:19

I\'d like to break apart a String by a certain length variable.
It needs to bounds check so as not explode when the last section of string is not as long as or longer th

12条回答
  •  孤城傲影
    2020-12-01 07:55

    I had the strange scenario where I had segmented a string, then rearranged the segments (i.e. reversed) before concatenating them, and then I later needed to reverse the segmentation. Here's an update to the accepted answer by @SLaks:

        /// 
        /// Split the given string into equally-sized segments (possibly with a 'remainder' if uneven division).  Optionally return the 'remainder' first.
        /// 
        /// source string
        /// size of each segment (except the remainder, which will be less)
        /// if dividing  into segments would result in a chunk smaller than  left at the end, instead take it from the beginning
        /// list of segments within 
        /// Original method at https://stackoverflow.com/questions/3008718/split-string-into-smaller-strings-by-length-variable 
        private static IEnumerable ToSegments(string str, int maxLength, bool remainderFirst = false) {
            // note: `maxLength == 0` would not only not make sense, but would result in an infinite loop
            if(maxLength < 1) throw new ArgumentOutOfRangeException("maxLength", maxLength, "Should be greater than 0");
            // correct for the infinite loop caused by a nonsensical request of `remainderFirst == true` and no remainder (`maxLength==1` or even division)
            if( remainderFirst && str.Length % maxLength == 0 ) remainderFirst = false;
    
            var index = 0;
            // note that we want to stop BEFORE we reach the end
            // because if it's exact we'll end up with an
            // empty segment
            while (index + maxLength < str.Length)
            {
                // do we want the 'final chunk' first or at the end?
                if( remainderFirst && index == 0 ) {
                    // figure out remainder size
                    var remainder = str.Length % maxLength;
                    yield return str.Substring(index, remainder);
    
                    index += remainder;
                }
                // normal stepthrough
                else {
                    yield return str.Substring(index, maxLength);
                    index += maxLength;
                }
            }
    
            yield return str.Substring(index);
        }//---  fn  ToSegments
    

    (I also corrected a bug in the original while version resulting in empty segment if maxLength==1)

提交回复
热议问题