Splitting a string

  • Thread starter Thread starter Tom C
  • Start date Start date
T

Tom C

Is there a faster, sleeker way to do the following? Maybe something
with regular expressions?

Dim Code As System.String = "(EntityName) Code"
Dim pos As Integer = Code.IndexOf(" "c)
Dim EntityName As System.String = Code.Substring(1, pos - 1I)
EntityName = EntityName.Replace("(", "")
EntityName = EntityName.Replace(")", "")
Code = Code.Substring(pos).Trim(" "c)
 
Tom C said:
Is there a faster, sleeker way to do the following? Maybe something
with regular expressions?

Dim Code As System.String = "(EntityName) Code"
Dim pos As Integer = Code.IndexOf(" "c)
Dim EntityName As System.String = Code.Substring(1, pos - 1I)
EntityName = EntityName.Replace("(", "")
EntityName = EntityName.Replace(")", "")
Code = Code.Substring(pos).Trim(" "c)

It would be easier to answer if you could get example(s) of the string you
are trying to parse.

LS
 
Tom said:
Is there a faster, sleeker way to do the following? Maybe something
with regular expressions?

Dim Code As System.String = "(EntityName) Code"
Dim pos As Integer = Code.IndexOf(" "c)
Dim EntityName As System.String = Code.Substring(1, pos - 1I)
EntityName = EntityName.Replace("(", "")
EntityName = EntityName.Replace(")", "")
Code = Code.Substring(pos).Trim(" "c)

Presumably you just want to get whatever is between the parentheses, and the
parentheses are possibly followed by a space and then possibly more stuff:

Dim s As String = "(EntityName) Code"
Console.WriteLine(s.Split(" "c)(0).Trim("()".ToCharArray))

Andrew
 
Is there a faster, sleeker way to do the following? Maybe something
with regular expressions?

Yes, definately regular expressions can do this cleaner. Are you trying to
strip out brackets?
 
Tom C said:
Is there a faster, sleeker way to do the following? Maybe something
with regular expressions?

Dim Code As System.String = "(EntityName) Code"
Dim pos As Integer = Code.IndexOf(" "c)
Dim EntityName As System.String = Code.Substring(1, pos - 1I)
EntityName = EntityName.Replace("(", "")
EntityName = EntityName.Replace(")", "")
Code = Code.Substring(pos).Trim(" "c)

Dim myString as String = "(EntityName) Code".Remove("(")
Dim myArray () as String = split(myString, ") ")
'** now myArray(0) contains "EntityName"
'**and
'**myArray(1) contains "Code"
 
Back
Top