Arrays

  • Thread starter Thread starter Travis
  • Start date Start date
T

Travis

I have made a string array and I am trying to copy every 5th letter from the
first element as characters
eg.


string [] my array = new string {"This is a string"};
string [] arraytocopyto;

//copy myarray[0][y] to new array [0][y] where Y is
// incrementing
arraytocopyto[0][y] = myarray[0][y];

this does not work though I get errors.
What is the easiset way to do this???

Travis
(e-mail address removed)
 
Travis,

You first have to allocate the array.

string[] myArray = new string[] { "This is a string" };
string[] arrayToCopyTo = new string[myArray.Count];

string temp = "";
for (int i = 0; i < myArray[0].Length; i += 5)
{
temp += myArray[0];
}

arrayToCopyTo[0] = temp;

-vJ
 
Travis said:
I have made a string array and I am trying to copy every 5th letter from the
first element as characters

eg.

string [] my array = new string {"This is a string"};
string [] arraytocopyto;

//copy myarray[0][y] to new array [0][y] where Y is
// incrementing
arraytocopyto[0][y] = myarray[0][y];

You can't modify a string - you can only modify a StringBuilder.
Strings are immutable. If you're trying to get a string which is every
fifth character from another string though, I'd ignore the arrays to
start with and write a method which takes a string and returns another
string appropriately. Then you can just do:

string[] myArray = ...;
string[] arrayToCopyTo = new string[myArray.Length];
for (int i=0; i < myArray.Length; i++)
{
arrayToCopyTo = GetEvery5thLetter (myArray);
}

where GetEvery5thLetter is the name of the method which constructs the
string.
 
Back
Top