Parse out data in a cell.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is for a Win form.

In a datagrid, I want to be able to extract a cell, and parse out the data
in the cell. The cell has a telephone number, and I'm able to get the full
telephone number.

Here is the code I have so far.

int rowNum = dgSportsman.CurrentCell.RowNumber;
object Cell8 = dgSportsman[rowNum, 7];

Cell8 returns the full telephone number. I want Cell8 to have the first 3
numbers, Cell9 to have the next 3 and Cell10 to have the last 4 numbers.
 
Hi Cadel,

If you can get the whole string for the phone number from the datagrid. You
can try to use Regular Expression to parse it to several substrings.
However, in this case, if the phone number is very simple, you can also
parse it use String.SubString method.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
What is the code to use regex? I went to the web site and I copied this
string but how I add to my WIN FORM?
^(?:(?<1>[(])?(?<AreaCode>[2-9]\d{2})(?(1)[)])(?(1)(?<2>[
])|(?:(?<3>[-])|(?<4>[
])))?)?(?<Prefix>[1-9]\d{2})(?(AreaCode)(?:(?(1)(?(2)[-
]|[-]?))|(?(3)[-])|(?(4)[- ]))|[- ]?)(?<Suffix>\d{4})$

What is the code to use Regular Expression to parse out it out to several
substrings?

You also said, String.Substring, if my variable is an object how is this done?

Below is my code so far. dgSportman is a data grid.

int rowNum = dgSportsman.CurrentCell.RowNumber;
object Cell1 = dgSportsman[rowNum, 0];
object Cell2 = dgSportsman[rowNum, 1];
object Cell3 = dgSportsman[rowNum, 2];
object Cell4 = dgSportsman[rowNum, 3];
object Cell5 = dgSportsman[rowNum, 4];
object Cell6 = dgSportsman[rowNum, 5];
object Cell7 = dgSportsman[rowNum, 6];

//Phone
object Cell8 = dgSportsman[rowNum, 7];
 
Hi Cadel,

Here, you can use Regex class to split the the whole string.

string Cell8 = dgSportsman[rowNum, 7].ToString();
System.Text.RegularExpressions.Regex phoneex = new
System.Text.RegularExpressions.Regex(@"^(?:(?<1>[(])?(?<AreaCode>[2-9]\d{2})
(?(1)[)])(?(1)(?<2>[])|(?:(?<3>[-])|(?<4>[])))?)?(?<Prefix>[1-9]\d{2})(?(Are
aCode)(?:(?(1)(?(2)[-]|[-]?))|(?(3)[-])|(?(4)[- ]))|[-
]?)(?<Suffix>\d{4})$");
string[] s = phoneex.Split(Cell8);

For more information, please check the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemtextregularexpressionsregexclasstopic.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top