How to convert numeric fields in an input line?

  • Thread starter Thread starter news.west.earthlink.net
  • Start date Start date
N

news.west.earthlink.net

I have been studying the book "Microsoft Visual C# .NET Step by Step" and am
now trying to develop a program in C#. I have some questions partially
because I do not have the MSDN Library with the help files.

How do you convert numeric fields in an input line like

string line = "123 456 7 89";

to the int values

int1 = 123;
int2 = 456;
int3 = 7;
int4 = 89;

In Visual Basic it would simply be

Input #1, int1, int2, int3, int4

-Harry
 
If you are imputting from a file, you have to do
four subsequuent reads. If you don't have MSDN
then you'd better buy a *simple* reference book.
(Not one of those ten kilogram books.)
 
Hi Harry,

One way to do it is this:

string line = "123 456 7 89";
string[] lines = line.split(" "); // convert the string to an array each
number is seperated by a space

int int1 = Convert.ToInt32( lines[0] );
int int2 = Convert.ToInt32( lines[1] );
int int3 = Convert.ToInt32( lines[2] );
int int4 = Convert.ToInt32( lines[3] );

Best Regards,

Robin Theilade
 
Thanks for your help. I had to add some code since some lines of input had
commas separating fields and some had two spaces between fields. It's all
working great now, bit I am always open to constructive criticism.

private StreamReader readSSFile;
readSSFile = new StreamReader(SSFile); // Open the file
.. . .
line = readSSFile.ReadLine(); // Read in next line
line = DeleteExtraDelimiters(line);
fields = line.Split(' '); // convert the string to an array, each number is
separated by a space
Pi = Convert.ToDouble( fields[0] );
P2 = Convert.ToDouble( fields[1] );
D2 = Convert.ToDouble( fields[2] );
StartI = Convert.ToInt32 ( fields[3] );
PrCnt = Convert.ToInt32 ( fields[4] );
Mc = Convert.ToInt32 ( fields[5] );
F2[0] = Convert.ToDouble( fields[6] );
PLn(" " + Pi + " " + P2 + " " + D2 + " " + StartI + " " + PrCnt + " " + Mc +
" " + F2[0]);
.. . .
private string DeleteExtraDelimiters(string st)
{
string line = st;
while (line[0] == ' ') line = line.Substring(1); // delete leading
spaces
for (int i = 0; i < line.Length; i++) // change , and " to a space
{
if (line == ',' || line == '\"')
{
line = line.Substring(0, i) + " " + line.Substring(i+1);
}
}
for (int i = 0; i < line.Length - 1; i++) // change " " to " "
{
if (line == ' ' && line[i+1] == ' ')
{
line = line.Substring(0, i) + line.Substring(i+1);
i--;
}
}
return line;
}

-Harry

Robin Theilade said:
Hi Harry,

One way to do it is this:

string line = "123 456 7 89";
string[] lines = line.split(" "); // convert the string to an array each
number is seperated by a space

int int1 = Convert.ToInt32( lines[0] );
int int2 = Convert.ToInt32( lines[1] );
int int3 = Convert.ToInt32( lines[2] );
int int4 = Convert.ToInt32( lines[3] );

Best Regards,

Robin Theilade

news.west.earthlink.net said:
I have been studying the book "Microsoft Visual C# .NET Step by Step"
and
am
now trying to develop a program in C#. I have some questions partially
because I do not have the MSDN Library with the help files.

How do you convert numeric fields in an input line like

string line = "123 456 7 89";

to the int values

int1 = 123;
int2 = 456;
int3 = 7;
int4 = 89;

In Visual Basic it would simply be

Input #1, int1, int2, int3, int4

-Harry
 
Hi Harry,

It looks okay although you might be able to optimize some of the code, I
recommend that you read up on some of the methods the string class/object
contains eg. Replace(). Here is a link to a reference
http://www.dotnet247.com/247reference/System/String/__members.

An example of removing all single quotes (')

str = str.Replace( "'", "" );

The first argument is what to replace and the second is what to replace it
with.

Best Regards,

Robin Theilade

Harry J. Smith said:
Thanks for your help. I had to add some code since some lines of input had
commas separating fields and some had two spaces between fields. It's all
working great now, bit I am always open to constructive criticism.

private StreamReader readSSFile;
readSSFile = new StreamReader(SSFile); // Open the file
. . .
line = readSSFile.ReadLine(); // Read in next line
line = DeleteExtraDelimiters(line);
fields = line.Split(' '); // convert the string to an array, each number is
separated by a space
Pi = Convert.ToDouble( fields[0] );
P2 = Convert.ToDouble( fields[1] );
D2 = Convert.ToDouble( fields[2] );
StartI = Convert.ToInt32 ( fields[3] );
PrCnt = Convert.ToInt32 ( fields[4] );
Mc = Convert.ToInt32 ( fields[5] );
F2[0] = Convert.ToDouble( fields[6] );
PLn(" " + Pi + " " + P2 + " " + D2 + " " + StartI + " " + PrCnt + " " + Mc +
" " + F2[0]);
. . .
private string DeleteExtraDelimiters(string st)
{
string line = st;
while (line[0] == ' ') line = line.Substring(1); // delete leading
spaces
for (int i = 0; i < line.Length; i++) // change , and " to a space
{
if (line == ',' || line == '\"')
{
line = line.Substring(0, i) + " " + line.Substring(i+1);
}
}
for (int i = 0; i < line.Length - 1; i++) // change " " to " "
{
if (line == ' ' && line[i+1] == ' ')
{
line = line.Substring(0, i) + line.Substring(i+1);
i--;
}
}
return line;
}

-Harry

Robin Theilade said:
Hi Harry,

One way to do it is this:

string line = "123 456 7 89";
string[] lines = line.split(" "); // convert the string to an array each
number is seperated by a space

int int1 = Convert.ToInt32( lines[0] );
int int2 = Convert.ToInt32( lines[1] );
int int3 = Convert.ToInt32( lines[2] );
int int4 = Convert.ToInt32( lines[3] );

Best Regards,

Robin Theilade

news.west.earthlink.net said:
I have been studying the book "Microsoft Visual C# .NET Step by Step"
and
am
now trying to develop a program in C#. I have some questions partially
because I do not have the MSDN Library with the help files.

How do you convert numeric fields in an input line like

string line = "123 456 7 89";

to the int values

int1 = 123;
int2 = 456;
int3 = 7;
int4 = 89;

In Visual Basic it would simply be

Input #1, int1, int2, int3, int4

-Harry
 
Back
Top