C#數組的合並拆分


1.合並拆分數組

///   <summary>
        
///  合並數組
        
///   </summary>
        
///   <param name="First"> 第一個數組 </param>
        
///   <param name="Second"> 第二個數組 </param>
        
///   <returns> 合並后的數組(第一個數組+第二個數組,長度為兩個數組的長度) </returns>
         public  string[] MergerArray( string[] First,  string[] Second)
        {
             string[] result =  new  string[First.Length + Second.Length];
            First.CopyTo(result,  0);
            Second.CopyTo(result, First.Length);
             return result;
        }

         ///   <summary>
        
///  數組追加
        
///   </summary>
        
///   <param name="Source"> 原數組 </param>
        
///   <param name="str"> 字符串 </param>
        
///   <returns> 合並后的數組(數組+字符串) </returns>
         public  string[] MergerArray( string[] Source,  string str)
        {
             string[] result =  new  string[Source.Length +  1];
            Source.CopyTo(result,  0);
            result[Source.Length] = str;
             return result;
        }

         ///   <summary>
        
///  從數組中截取一部分成新的數組
        
///   </summary>
        
///   <param name="Source"> 原數組 </param>
        
///   <param name="StartIndex"> 原數組的起始位置 </param>
        
///   <param name="EndIndex"> 原數組的截止位置 </param>
        
///   <returns></returns>
         public  string[] SplitArray( string[] Source,  int StartIndex,  int EndIndex)
        {
             try
            {
                 string[] result =  new  string[EndIndex - StartIndex+ 1];
                 for ( int i =  0; i <= EndIndex - StartIndex; i++) result[i] = Source[i+StartIndex];
                 return result;
            }
             catch (IndexOutOfRangeException ex)
            {
                 throw  new Exception(ex.Message);
            }
        }

         ///   <summary>
        
///  不足長度的前面補空格,超出長度的前面部分去掉
        
///   </summary>
        
///   <param name="First"> 要處理的數組 </param>
        
///   <param name="byteLen"> 數組長度 </param>
        
///   <returns></returns>
         public  string[] MergerArray( string[] First,  int byteLen)
        {
             string[] result;
             if (First.Length > byteLen)
            {
                result =  new  string[byteLen];
                 for ( int i =  0; i < byteLen; i++) result[i] = First[i + First.Length - byteLen];
                 return result;
            }
             else
            {
                result =  new  string[byteLen];
                 for ( int i =  0; i < byteLen; i++) result[i] =  "   ";
                First.CopyTo(result, byteLen-First.Length);
                 return result;
            }
        }

2.調用

string[] student1 = {  " a "" b "" c "" d "" e "" f "};
string[] student2 = {  " 0 "" 1 "" 2 "" 3 "" 4 "" 5 "" 6 "" 6 "" 1 "" 8 "" 16 "" 10 "" 45 "" 37 "" 82 " };
string student3 =  " Test ";
// 兩個數組相加 結果--> { "a", "b", "c", "d", "e", "f","0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16", "10", "45", "37", "82" };
string[] Merger1 = MergerArray(student1, student2);
// 數組+字符串  結果-->{ "a", "b", "c", "d", "e", "f","Test"};
string[] Merger2 = MergerArray(student1, student3);
// 不足10位的,前面補空格 結果-->{" ", " ", " ", " ", "a", "b", "c", "d", "e", "f"};
string[] Merger3 = MergerArray(student1,  10);
// 超過3位的,只取最后3后 結果-->{"d", "e", "f"};
string[] Merger4 = MergerArray(student1,  3);
// 截取部分數組 結果-->{"c", "d", "e"};
  string[] SplitArray1 = SplitArray(student1,  24);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM