Reading (string)

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I have a command line application and i want to read a string that
contains couple of words.I should read this line untill I reach a group
of charecters such as {b,c,d}.
How can I do it?
 
Hello,
I have a command line application and i want to read a string that
contains couple of words.I should read this line untill I reach a group
of charecters such as {b,c,d}.
How can I do it?
Read in the whole line using Console.ReadLine(). Once you have the
whole line in memory then you can parse out the bits that you want for
your various purposes.

rossum
 
I think you are looking for something like the following.

================================================
class Program
{
static char[] terminatingChars = { 'b', 'c', 'd' };

static void Main(string[] args)
{

char input;

do
{
input = Convert.ToChar(Console.ReadKey().Key);
//The above line should be replaced with equivalent of
scanf("%c",c);

//what ever you want to do with the input ....
}while(Terminate(input));

Console.ReadLine();
}

public static bool Terminate(char inp)
{
foreach (char tc in terminatingChars)
if (tc == inp)
return false;

return true;
}

================================

I do not exactly know how to replace

input = Convert.ToChar(Console.ReadKey().Key);

with something like

scanf("%c",c ) as in C


Thanks
-Srinivas.
 
csharpula csharp said:
Hello,
I have a command line application and i want to read a string that
contains couple of words.I should read this line untill I reach a group
of charecters such as {b,c,d}.
How can I do it?
Here is some code that will do the job. Regular expressions are your friend.
textout[0] will hold the string you want.


string textin = "This is one method to do it. a b c should be clipped";
Regex reg = new Regex("[cba]");

string[] textout = System.Text.RegularExpressions.Regex.Split(textin,
reg.ToString());

Console.WriteLine (textout[0]);

Console.ReadKey();



Roy Berube
 
Roy A Berube said:
string textin = "This is one method to do it. a b c should be clipped";
Regex reg = new Regex("[cba]");

string[] textout = System.Text.RegularExpressions.Regex.Split(textin,
reg.ToString());

Console.WriteLine (textout[0]);

Console.ReadKey();
It might also help to declare this -

using System.Text.RegularExpressions;



Roy B
 
Thank you all but I have an update at my problem,I need to read the line
(including spaces) untill -a -b -c (string and not char).How to do it?
 
csharpula csharp said:
Thank you all but I have an update at my problem,I need to read the line
(including spaces) untill -a -b -c (string and not char).How to do it?

Use the regex code I posted earlier, and replace the regex declararation
with this:
Regex reg = new Regex(@"-a -b -c");


It will then look for the exact match to what is inside the quotation marks.
Regular expressions can look cryptic, but are very powerful.
Roy Berube
 
Back
Top