Solved

c# how do I cut down an array to a specific size?

Posted on 2011-09-27
4
579 Views
Last Modified: 2013-12-17
I want a function where I can pass in an array of strings and pass back an array of size 5 or smaller.  So if I pass in an array of size 100, return an array of size 5.  If I pass in an array of size 3, return the full array of size 3.
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 36713303
It should be as simple as:
static string[] GetTrimmedArray(string[] theArr, int trimSize)
{
    if (theArr == null) throw new ArgumentException("Input array cannot be null.");

    if (trimSize < 0) throw new ArgumentException("Size of result array cannot be negative.");

    if (trimSize > theArr.Length) trimSize = theArr.Length;

    string[] result = new string[trimSize];

    for (int i = 0; i < trimSize; i++) { result[i] = theArr[i]; }

    return result;
}

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 125 total points
ID: 36713304
static string[] YourFunction ( String[] x )
{

string[] y;
int count;

if ( x.Length > 5 )
   {
   count = 5;
   }
else
   {
   count = x.Length;
   }

y = new string[count];

for ( int index = 0; (index < count);  index++ )
   {
   y[index] = x[index];
   }

return y;

}

Open in new window

0
 
LVL 15

Accepted Solution

by:
x77 earned 125 total points
ID: 36713337
.Net Array hash method  Array.Resize.
It allows grow and also Trunc.

  array.resize(myArray, 5) - Now MyArray has 5 items. Itmaintains elements 0 to 4.
  Note that original array is not afected, we pass MyArray by Ref.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 125 total points
ID: 36714444
hi,

If u do not know which all index the data is placed in an array, then u need to check through out the array and then construct a new array to the intended size.
static string[] TrimArray(string[] array, int trimSize)
        {
            // Perform primary validations on input parameters

            List<string> dataList = new List<string>();

            foreach (string data in array)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    dataList.Add(data);

                    if (trimSize == dataList.Count)
                    {
                        // Max size for trim requested is attained.
                        break;
                    }
                }
            }

            return dataList.ToArray();
        }

Open in new window

0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 auto inserted code 12 88
vb.net application has warrnings about VB6 calls. 2 47
C# LINQ query question 11 32
AD Computer Objects in VB .Net 2 17
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question