Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Given a string called Format which would be of the form

string Format = �[Col1] : [Col2] : [Col3] : ���.. [ColX]�

 

I need to create a method to count the # of Columns the string defines [located between the [x]'s].

 

Any clues?

Edited by Shaitan00
Posted (edited)

I would use the Split method of the string...

If you use it with the Char ":" it will create an array with all [x]'s, then you just have to "ask" the array how many object it has...

 

This will show a messagebox with the counting:

Dim a() As String = Format.Split(":")
MessageBox.Show(a.Length)

 

Alex :D

Edited by AlexCode
Software bugs are impossible to detect by anybody except the end user.
Posted

Amazing idea, that is exactly the type of method I needed [parsing was driving me nutz].

 

This is the code I am trying to use (given your example):

string[] arrCols = format.Split(" : ");

 

 

However it generates the following errors:

- The best overloaded method match for 'string.Split(params char[])' has some invalid arguments

- Argument '1': cannot convert from 'string' to 'char[]'

 

 

Any clues?

Posted

the parameter on the Split method can't be a string... it has the be a char ... and chars can't have more than 1 character...

 

use:

string[] arrCols = format.Split(":");

 

if you then want to take spaces use the Trim method on the string...

 

 

Alex :D

Software bugs are impossible to detect by anybody except the end user.
  • *Experts*
Posted

dynaimc_sysop, that will return a bunch of blank array elements,

because the string will be split at any one of those character.

So, for example, the array elements would be "", "Col1", " ", " ",

etc. I believe the best method is to use the Split method of the

Regex class.

 

string[] columns = System.Text.RegularExpressions.Regex.Split("[Col1] : [Col2] : [Col3] : [ColX]", " : ")

foreach (string column in columns) {
 // do whatever with column
}

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...