Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, March 28, 2012

Substring

In an aspx (asp2.0) page in C# I have a string array. The array contains
letters separated into two groups by a dash (-). I want to obtain the
letters before the dash. I use the following code. Strangely, when the
array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
What's going on ? What is changing the value of variable mn ? Thanks,
Jim
int i = 32;
string s1;
string s2;
s1 = Ante_Arr[i];
int mn = s1.IndexOf("-");
Label3.Text = mn.ToString(); // the displayed value is 3
try
{
s2 = s1.Substring(1, mn); // throws an error: length can not be less
than zero
}
catch
{
Label4.Text = mn.ToString(); // the displayed value is -1
}Maybe it's scope?
Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
the value of mn before the s2 = s1.Substring(1, mn); line.
"Jim McGivney" <mcgiv1@.no-spam.sbcglobal.net> wrote in message
news:O7EM879dGHA.3348@.TK2MSFTNGP03.phx.gbl...
> In an aspx (asp2.0) page in C# I have a string array. The array contains
> letters separated into two groups by a dash (-). I want to obtain the
> letters before the dash. I use the following code. Strangely, when the
> array has a value of "MOD-AS" Label3 displays "3" while Label4
> displays -1.
> What's going on ? What is changing the value of variable mn ? Thanks,
> Jim
> int i = 32;
> string s1;
> string s2;
> s1 = Ante_Arr[i];
> int mn = s1.IndexOf("-");
> Label3.Text = mn.ToString(); // the displayed value is 3
> try
> {
> s2 = s1.Substring(1, mn); // throws an error: length can not be less
> than zero
> }
> catch
> {
> Label4.Text = mn.ToString(); // the displayed value is -1
> }
>

Substring

In an aspx (asp2.0) page in C# I have a string array. The array contains
letters separated into two groups by a dash (-). I want to obtain the
letters before the dash. I use the following code. Strangely, when the
array has a value of "MOD-AS" Label3 displays "3" while Label4 displays -1.
What's going on ? What is changing the value of variable mn ? Thanks,
Jim

int i = 32;
string s1;
string s2;
s1 = Ante_Arr[i];
int mn = s1.IndexOf("-");
Label3.Text = mn.ToString(); // the displayed value is 3
try
{
s2 = s1.Substring(1, mn); // throws an error: length can not be less
than zero
}
catch
{
Label4.Text = mn.ToString(); // the displayed value is -1
}Maybe it's scope?

Put the int mn = s1.IndexOf("-"); line into the try block, or take a look at
the value of mn before the s2 = s1.Substring(1, mn); line.

"Jim McGivney" <mcgiv1@.no-spam.sbcglobal.net> wrote in message
news:O7EM879dGHA.3348@.TK2MSFTNGP03.phx.gbl...
> In an aspx (asp2.0) page in C# I have a string array. The array contains
> letters separated into two groups by a dash (-). I want to obtain the
> letters before the dash. I use the following code. Strangely, when the
> array has a value of "MOD-AS" Label3 displays "3" while Label4
> displays -1.
> What's going on ? What is changing the value of variable mn ? Thanks,
> Jim
> int i = 32;
> string s1;
> string s2;
> s1 = Ante_Arr[i];
> int mn = s1.IndexOf("-");
> Label3.Text = mn.ToString(); // the displayed value is 3
> try
> {
> s2 = s1.Substring(1, mn); // throws an error: length can not be less
> than zero
> }
> catch
> {
> Label4.Text = mn.ToString(); // the displayed value is -1
> }

substring of an array. Weird results!

I've done this many times but this is a first for this problem. I have a string which I do a split on a specific char, the $ sign.

{D}United States,Lettie,C,Letas,52 Alpha Lane,4 Schilling Lane,Test City,District of Columbia,12345-
1234,betabetty@dotnet.itags.org.here.com,101-1010,121-1212,555-555-5555,Bob Beta
{B}5/30/1976,Female,5'4,110,27,118,74,127,42,85
{L}134,bettyB01,bBob1231950,What is your favorite snack?,Gum drops
$

I then want to get specific parts of the string so I do an substring on this only to get the:

Index and length must refer to a location within the string.
Parameter name: length

error message on this bit of code. b = {B} and l = {L} which can been seen in the text above.

int t1 = separateParticipants[0].IndexOf(b);
int t2 = separateParticipants[0].IndexOf(l);
litUploadError.Text = separateParticipants[0].Substring(t1,t2);

A couple things I've tested that worked correctly.

litUploadError.Text = separateParticipants[0].Substring(0,t1);

litUploadError.Text = separateParticipants[0].Substring(0,t2);

int t1 = 0;
int t2 = separateParticipants[0].IndexOf(l);
litUploadError.Text = separateParticipants[0].Substring(t1,t2);

Since they both worked with 0, it should mean that t1 and t2 exist, so why is it that when I try to get the data between t1 and t2, it gives the error?

thanks ^_^

Your problem is that you are requesting too many characters. t2 is the index of {L}, but the number of characters that come after t2 is less than the number that come after it.

To be more clear, the definition of paramters for substring is (start Index, Number Of characters); it doesn't fetch the string between two given indices.

Your last piece of code should be:

litUploadError.Text = separateParticipants[0].Substring(t1, (t2 - t1) +1);


LOL! You are so right. Can't believe I missed that. Guess I need another beer. Thanks a bunch. :)

Tuesday, March 13, 2012

Swaping rows in a 2 dimensional array

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;/// <summary>/// Summary description for Board/// </summary>public class Sudoku{private static Random rnd;const int colomns = 6;const int rows = 6;//encapsulationstatic int[,] Luach=new int[colomns,rows];//properties //constructorspublic Sudoku(){//// TODO: Add constructor logic here//}public static void InitGame(Label L) {int z=1;for (int r = 0; r < rows; r++) {for (int c = 0; c < colomns; c++) { Luach[r, c] = z; z++;if (z == 7) z = 1; L.Text += Luach[r, c].ToString() +" "; } z++; L.Text +="#"; } }public static void SwapRows() { }}
Hello !

I'm completely novice and I was given a task at school:

I

I wrote the function InitGame in my class "Sudoku" and that function writes numbers ranging from 1 to 6 when the parameter is "z" and as you can see it goes from 1 to 7 and when it gets
to "7" and goes back to "1"
Now I have a 2 dimensional array called "Luach"
the paramet for rows is "r" and for colomns is "c".

Now the next thing I have to do (and here I face difficulty) is to swap the rows N times (Using the Random)

I print the numbers in onr row and # separates between each rows (I have a 2 dimensional array).

I should write the code for it in a separate function in the class called "SwapRows:

So please tell me what to write so that I can swap the rows N times.

Thanks !!!

// -- SwapRows ------------ // // Input: Two Dimensional array, which rows to swapprivate void SwapRows(int[,] AnArray,int RowA,int RowB) {// creat a temp rowint[] TempArray =new int[AnArray.GetLength(1)];// Store Row Afor (int Col = 0; Col < AnArray.GetLength(1); Col++) TempArray[Col] = AnArray[RowA, Col];// Copy Row B onto Row Afor (int Col = 0; Col < AnArray.GetLength(1); Col++) AnArray[RowA, Col] = AnArray[RowB, Col];// Copy the Temp row onto Row Bfor (int Col = 0; Col < AnArray.GetLength(1); Col++) AnArray[RowB, Col] = TempArray[Col]; }