0

If I got an array like:

string[] test = new string[5] { "hello", "world", "test", "world", "world"};

How can I make a new array out of the ones that is the same string, "world" that is where you on before hand know how many there are, here 3?

I was thinking of something like:

string[] newArray = new string[3];

        for (int i = 0; i < 5; i++)
        {
            if (test[i].Contains("world"))
            {
                newArray[i] = test[i];
            }
        }

The problem is here: newArray[i] = test[i];

Since it's iterating from 0 to 4, there's gonna be an error since newArray is limited to 3.

How do solve this?

EDIT: I need it to be that from test (the old array) position 1, 3 and 4 should be stored at 0, 1 and 2 in the newArray.

7
  • Could you write the output you want exactly as you did for the input? It would help us (at least me) to understand exactly what you want. Commented Jul 25, 2013 at 22:56
  • @CédricBignon I would like the output to be something like "newArray contains the following strings: world, world, world". Is that how you meant? Commented Jul 25, 2013 at 22:58
  • @Erik It was more like string[] output = new string[3] { "world", "world", "world"}. What do you want as output for the following input { "hello", "world", "hello", "hello", "test", "my world", "world"}? Commented Jul 25, 2013 at 23:00
  • @Erik: that's exactly what the 2 answers with 0 upvotes tried to do from the start. Not sure if that's what you actually want though. Commented Jul 25, 2013 at 23:08
  • Then I'd want string[] output = new string[3] { "world", "world", "world"} since it's still 3 strings that contains "world". Thanks. Commented Jul 25, 2013 at 23:08

5 Answers 5

5

You want to use Linq:

var newArray = test.Where(x => x.Contains("world")).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

4

Use a List<string> instead:

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

    for (int i = 0; i < 5; i++)
    {
        if (test[i].Contains("world"))
        {
            newList.Add(test[i]);
        }
    }

If you really need it as an array later.. convert the list:

string[] newArray = newList.ToArray();

Comments

1

You are using the same index i for both test and newArray. I would suggest you create another counter variable and increment it:

string[] newArray = new string[3];
int counter = 0;

for (int i = 0; i < 5; i++)
{
    if (test[i].Contains("world"))
    {
        newArray[counter] = test[i];
        counter++;
    }
}

3 Comments

@Erik - this will work, I would also extend this further and break out of the for loop if counter is >= newArray.Length
I'm sorry again. I misread, it's late here, hehe. Your logic is correct, I'll see if I can apply this logic.
Accepted this as it was the simplest applicable and working solution in my case, even though most answers will work just as great (and some even better).
1

This isn't technically your question but if you wish to make a load of arrays based of those with the same word you could do

test.GroupBy(x => x).ToList();

this will give you a List of Lists.. with your test data this will be

list1 - hello
list2 - world world world
list3 - test

Example use

var lists =  test.GroupBy(x => x).ToList();
foreach(var list in lists)
{
     foreach(var str in list)
     {
         Console.WriteLine(str);
     } 
     Console.WriteLine();
}

Comments

1

With an extra helper index variable

    string[] newArray = new string[3];

    for (int i = 0, j = 0; i < 5; i++)
    {
        if (test[i].Contains("world"))
        {
            newArray[j++] = test[i];
            if (j >= newArray.Length)
                break;
        }
    }

9 Comments

this won't work if there are more than 3 strings with "world". edit: didn't see that the question said you already know there are 3
@Alden: well that was his requirement, not mine. He defined array at 3. Read his question conditions, I am just answering.
or more than 5 elements in the array, and OP says if his array was like that, meaning thats not his actual data
@Alden The question said, "where you on before hand know how many there are, here 3".
Personally I think its awful coding including two for loops in the same line as you have. I also still don't think your code would work if there were suddenly 4 "worlds", OP would have to update this code which is just tedious and error prone
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.