help with array.convert

  • Thread starter Thread starter SIS01
  • Start date Start date
S

SIS01

Hi,

Please helpme

I want to split this line:

string line = "Davis , Jones, Beckett , Jordan , Kennt "

into an array of their trimmed versions:

"Davis"
"Jones"
"Beckett"
"Jordan"
"Kennt"


I've tried several solutions but none resolves it.

Dim linea As String = "Davis , Jones, Beckett , Jordan , Kennt "
Dim delimiter As String = ","
Dim nombress As String() = linea.Split(delimiter.ToCharArray())


Array.ForEach(nombress, Function(p) p.Trim()) ' does not work
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

Array.ConvertAll(nombress, New Converter(Of String, String)(Function(c)
c.Trim())) ' does not work
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

For Each n In nombress
n = n.Trim ' does not work
Next
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

Thanks in advance
 
Keep it simple:

Dim line As String = "Davis , Jones, Beckett , Jordan , Kennt "
Dim names() As String = line.Split(","c)
For i As Integer = 0 To names.Length - 1
names(i) = names(i).Trim
Next
 
.... or if you really want to use LINQ:

Dim line = "Davis , Jones, Beckett , Jordan , Kennt "
Dim names = From p In line.Split(","c) Select p.Trim
 
Thanks,

I test it but i've got a error.
Dim namesS As String() = From p In linea.Split(delimiter.ToCharArray())
Select p.Trim

Cant convert an obtjet of type
'WhereSelectArrayIterator`2[System.String,System.String]' to type
'System.String[]'.

What can i do?
 
SIS01 said:
I test it but i've got a error.
Dim namesS As String() = From p In linea.Split(delimiter.ToCharArray())
Select p.Trim

Cant convert an obtjet of type
'WhereSelectArrayIterator`2[System.String,System.String]' to type
'System.String[]'.

What can i do?

The LINQ gives you an IEnumerable(Of String), whereas you have defined
namesS as an array. You can tell it to convert it to an array:

Dim namesS As String() = (From p In linea.Split(delimiter.ToCharArray())
Select p.Trim).ToArray
 
Dim x = Split("Davis , Jones, Beckett , Jordan , Kennt ".Replace("
", ""), ",")

Success

Cor

"SIS01" wrote in message
Hi,

Please helpme

I want to split this line:

string line = "Davis , Jones, Beckett , Jordan , Kennt "

into an array of their trimmed versions:

"Davis"
"Jones"
"Beckett"
"Jordan"
"Kennt"


I've tried several solutions but none resolves it.

Dim linea As String = "Davis , Jones, Beckett , Jordan , Kennt "
Dim delimiter As String = ","
Dim nombress As String() = linea.Split(delimiter.ToCharArray())


Array.ForEach(nombress, Function(p) p.Trim()) ' does not work
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

Array.ConvertAll(nombress, New Converter(Of String, String)(Function(c)
c.Trim())) ' does not work
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

For Each n In nombress
n = n.Trim ' does not work
Next
Array.ForEach(nombress, Function(p) WriteLine("action: -" & p & " - "))

Thanks in advance
 
Thanks
it's a good idea
but fails when the line contains names with spaces
"Alan Davis , Peter Fall , Wendy Truman, .... "

AlanDavis
PeterFall
WendyTruman

....
Thanks again
 
Yea but that was not the problem.

You reply like if you ask how can I add 3 and we tell
dim x = whatever + 3

And then you tell it fails if I want to add 2



"SIS01" wrote in message
Thanks
it's a good idea
but fails when the line contains names with spaces
"Alan Davis , Peter Fall , Wendy Truman, .... "

AlanDavis
PeterFall
WendyTruman

....
Thanks again
 
Back
Top