help with array.convert

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
 
A

Andrew Morton

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
 
A

Andrew Morton

.... 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
 
S

SIS01

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?
 
A

Andrew Morton

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
 
C

Cor

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
 
S

SIS01

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
 
C

Cor

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top