9 replies

Last post Oct 25, 2014 05:14 AM by oned_gk

• Shibly

Member

69 Points

140 Posts

Insert string into array index

Oct 25, 2014 02:04 AM|Shibly|LINK

I've a array like string[] b = {"ABC03J", "CCC03J", "ABC03J", "ABC03J"};

I want to make every two string into a one string like,  "ABC03J", "CCC03J" this two string convert "ABC03JCCC03J"

and also insert this string into a 2D array. In 2D array I want to insert two character into one index, Like, this string I want: string[,] ab = { {"AB","C0","3J"}, {.......} };

How I and suppose to do that. Can any one help me please.....

csharp .net

• oned_gk

All-Star

51721 Points

15281 Posts

Re: Insert string into array index

Oct 25, 2014 02:20 AM|oned_gk|LINK

```        string[] b = { "ABC03J", "CCC03J", "ABC03J", "ABC03J" };
string[] c = new string[b.Length / 2];
for (int i=0; i < b.Length; i++)
{
c[i / 2] += b[i];
}```

csharp .net

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• Shibly

Member

69 Points

140 Posts

Re: Insert string into array index

Oct 25, 2014 02:47 AM|Shibly|LINK

Thank you for your reply and it's working but how to insert 2 character from this string into one 2D array index. like,

string[] c = {"ABC03JCCC03J", "ABC03JABC03J"}; to string[,] d = {{"AB","C0","3J","CC","C0","3J"} , {"AB","C0","3J","AB","C0","3J"} };

csharp .net

• gerrylowry

Star

14297 Points

5797 Posts

Re: Insert string into array index

Oct 25, 2014 03:26 AM|gerrylowry|LINK

@Shibly  is this a homework problem?  if it is, oned_gk has done only the first half of your homework.  You do need to thank him.

What approaches have you tried?

Can you make  oned_gks code more efficient?    TIMTOWTDI

```String[] b = { "ABC03J", "CCC03J", "ABC03J", "ABC03J" };
String[] c = new string[b.Length / 2];
for (Int32 i=0; i < (c.Length); i++)
{
Int32 j=2*i;
c[i] = b[j] + b[j+1];
}```

Which is more efficient?

csharp .net

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• smirnov

All-Star

35159 Points

9075 Posts

Re: Insert string into array index

Oct 25, 2014 03:37 AM|smirnov|LINK

agree with Gerry, you need to try to do it yourself

Depends on your target you could also try to use List, e.g.

```List<string[]> l = new List<string[]>();

double parts = 2;
int k = 0;

foreach(string s in c)
{
var output = s
.ToLookup(x => Math.Floor(k++ / parts))
.Select(e => new String(e.ToArray()));

}```

If you need to convert final list to a 2d array, read http://stackoverflow.com/questions/9774901/how-to-convert-list-of-arrays-into-a-2d-array

csharp .net

• gerrylowry

Star

14297 Points

5797 Posts

Re: Insert string into array index

Oct 25, 2014 03:42 AM|gerrylowry|LINK

Shibly

... how to insert 2 character from this string into one 2D array index. like,

string[] c = {"ABC03JCCC03J", "ABC03JABC03J"}; to string[,] d = {{"AB","C0","3J","CC","C0","3J"} , {"AB","C0","3J","AB","C0","3J"} };

Hint:

define an array [2,6]

use Substring to assign

d[0,0]   d[0,1]    d[0,2]   d[0,3] ...  to respective parts  c[0]

et cetera

EDIT:

Shibly, the code by smirnov works BUT do you understand it?

Can you explain it to your teacher if this really is a homework assignment?

is there a more efficient approach?

end EDIT.

csharp .net

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• Shibly

Member

69 Points

140 Posts

Re: Insert string into array index

Oct 25, 2014 04:05 AM|Shibly|LINK

@gerrylowry this is not my homework. This is a small part of my project. I just don't understand how to do this. Yeah, I thanked him.

And also thank you for your help.

Can you give me some kind of hints how to do the other half. Because My input are not fixed for every time. This input given by clients.

csharp .net

• smirnov

All-Star

35159 Points

9075 Posts

Re: Insert string into array index

Oct 25, 2014 04:34 AM|smirnov|LINK

Shibly

Because My input are not fixed for every time.

what about example I gave you above? it could work with any input.

csharp .net

• gerrylowry

Star

14297 Points

5797 Posts

Re: Insert string into array index

Oct 25, 2014 04:37 AM|gerrylowry|LINK

@Shibly you're welcome.

Your peers, myself included do not mind helping our peers with homework ... we want to help you learn so that you in turn can help others learn.

it's important imho to always try to improve one's code.

```String[] b = { "ABC03J", "CCC03J", "ABC03J", "ABC03J" };
String[] c = new string[b.Length / 2];
String[,] d = new String [2,6];
for (Int32 i=0; i < (c.Length); i++)
{
Int32 j=2*i;
c[i] = b[j] + b[j+1];
d[i,0] = c[i].Substring( 0,2);
d[i,1] = c[i].Substring( 2,2);
d[i,2] = c[i].Substring( 4,2);
d[i,3] = c[i].Substring( 6,2);
d[i,4] = c[i].Substring( 8,2);
d[i,5] = c[i].Substring(10,2);
}```

Shibly

Can you give me some kind of hints how to do the other half

Shibly

My input are not fixed for every time. This input given by clients.

Likewise, what do you mean exactly?

Questions:

• is each item always of the form 6 characters?
• are your strings always in pairs?
• after you build a string, are you always going to break it into 6 groups of length 2?
• how are you actually getting this data from your clients?

edit:

```String[] b = { "ABC03J", "CCC03J", "ABC03J", "ABC03J", "x1x2x3", "y1y2y3", "1p2p3p", "1q2q3q", "jklmno", "pqrstu" };
String[] c = new string[b.Length / 2];
String[,] d = new String [c.Length,6];
for (Int32 i=0; i < (c.Length); i++)
{
Int32 j=2*i;
c[i] = b[j] + b[j+1];
d[i,0] = c[i].Substring( 0,2);
d[i,1] = c[i].Substring( 2,2);
d[i,2] = c[i].Substring( 4,2);
d[i,3] = c[i].Substring( 6,2);
d[i,4] = c[i].Substring( 8,2);
d[i,5] = c[i].Substring(10,2);
}```

smirnov''s code also works with this change:

```String[] b = { "ABC03J", "CCC03J", "ABC03J", "ABC03J", "x1x2x3", "y1y2y3", "1p2p3p", "1q2q3q", "jklmno", "pqrstu" };
```

edit end.

edit2:

smirnov''s code also works with this change too:

`String[] b = { "AB", "CC", "ABC0", "ABC0", "x1x2x3", "y1y2y3", "1p2p3p1q2q3q", "jklmnopqrstu" };`

output:

```ABCC
ABC0ABC0
x1x2x3y1y2y3
1p2p3p1q2q3qjklmnopqrstu
AB CC
AB C0 AB C0
x1 x2 x3 y1 y2 y3
1p 2p 3p 1q 2q 3q jk lm no pq rs tu```

end edit2.

edit3:

Shibly can you see one way in which the solution by smirnov does not meet your specification?  is that a sacrifice you can make?

end edit3.

csharp .net

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• oned_gk

All-Star

51721 Points

15281 Posts

Re: Insert string into array index

Oct 25, 2014 05:14 AM|oned_gk|LINK

```        string[] c = {"ABC03JCCC03J", "ABC03JABC03J"};
string[,] d = new string[c.Length,6];
for (int i = 0; i < c.Length; i++)
{
for(int j=0;j<6;j++)
{
d[i, j] = c[i].Substring(j*2, 2);
}
}```

csharp .net

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer