Concatenating Chars in an Array

E

eBob.com

I know it wouldn't be hard to write, but I have been unable to find a method
which will take elements of a character array and concatenate them into a
string. I've looked at both String members and Char members and haven't
found any such method. But it seems so obvious. So I figure I just haven't
hit upon the right Google search arguments.

I just want a function which will concatenate some consecutive elements of a
Char array. I know it must exist, I just haven't been able to find it.

Thanks, Bob
 
J

J.B. Moreno

eBob.com said:
I know it wouldn't be hard to write, but I have been unable to find a method
which will take elements of a character array and concatenate them into a
string. I've looked at both String members and Char members and haven't
found any such method. But it seems so obvious. So I figure I just haven't
hit upon the right Google search arguments.

I just want a function which will concatenate some consecutive elements of a
Char array. I know it must exist, I just haven't been able to find it.

Encoding.ASCII.GetString
 
S

Stephany Young

Dim _string = New String(_charrarray, _x, _y)

where:

_chararray is your Array of Char
_x is the index of the first element
_y is the number of elements
 
K

kimiraikkonen

I know it wouldn't be hard to write, but I have been unable to find a method
which will take elements of a character array and concatenate them into a
string. I've looked at both String members and Char members and haven't
found any such method. But it seems so obvious. So I figure I just haven't
hit upon the right Google search arguments.

I just want a function which will concatenate some consecutive elements of a
Char array. I know it must exist, I just haven't been able to find it.

Thanks, Bob

String type has constructor that may taka a char array as argument, i
hope that does:

' Define your char array
Dim charArr() As Char = _
{"b", "a", "s", "i", "c"}
Dim str As New String(charArr)
' Here is your concatenated string
MsgBox(str)


HTH,

Onur Güzel
 
C

Cor Ligthert [MVP]

Hi,

What are those VB conversion functions strong.

Dim charar As Char() = {"1"c, "2"c, "3"c}
Dim B As String = CStr(charar)

Cor
 
E

eBob.com

Geez, I did look at all String methods but never thought to look at the
constructor! That's the solution I like best, but thanks to ALL responders.
You guys are great.

Thanks, Bob
 
H

Herfried K. Wagner [MVP]

eBob.com said:
I know it wouldn't be hard to write, but I have been unable to find a
method which will take elements of a character array and concatenate them
into a string. I've looked at both String members and Char members and
haven't found any such method.

There's nothing easier than that:

\\\
Dim c() As Char = {"A"c, "B"c, "C"c}
Dim s As String = c
MsgBox(s)
///

VB provides an implicit widening conversion for 'Char()' -> 'String', even
with 'Option Strict On'. Widening conversions never fail because the value
domain of the target type is a superset of the value domain of the source
type.
 
E

eBob.com

Herfried K. Wagner said:
There's nothing easier than that:

\\\
Dim c() As Char = {"A"c, "B"c, "C"c}
Dim s As String = c
MsgBox(s)
///

VB provides an implicit widening conversion for 'Char()' -> 'String', even
with 'Option Strict On'. Widening conversions never fail because the
value domain of the target type is a superset of the value domain of the
source type.
Thanks Herfried. That's interesting and useful. I need only so many
characters starting at a specific offset within the char array. As someone
else pointed out a String constructor overload can do exactly that. But I
like the capability you've made me aware of and I know I'll be using it
soon.

Thanks again, Bob
 
G

Göran Andersson

Herfried said:
There's nothing easier than that:

\\\
Dim c() As Char = {"A"c, "B"c, "C"c}
Dim s As String = c
MsgBox(s)
///

VB provides an implicit widening conversion for 'Char()' -> 'String',
even with 'Option Strict On'. Widening conversions never fail because
the value domain of the target type is a superset of the value domain of
the source type.

As it's an implicit conversion, you can make it explicit if you want
without changing the resulting code:

Dim s As String = New String(c)

More to type, but it more clearly shows what's actally going on.

:)
 
C

Cor Ligthert[MVP]

Goran,

Yours is a C# aproach, which has a lack of the strong Conversion methods
from VB. have a look at my answer.

Although I find the one from Herfried very much better.

Cor
 
G

Göran Andersson

Cor said:
Goran,

Yours is a C# aproach, which has a lack of the strong Conversion methods
from VB. have a look at my answer.

Although I find the one from Herfried very much better.

Cor

Not at all. It's the .NET approach. It's not specific to any language,
which is proven by the fact that it works just a well in VB.

The .NET approach doesn't lack anything in this case, as the VB approach
doesn't add anything. It just works as a wrapper.
 
C

Cor Ligthert[MVP]

Goran,

Do you want to say that VB is not .Net while C# is?

I assume that in your idea the System.Net namespaces means .Net.

(But it is not)

Cor
 
H

Herfried K. Wagner [MVP]

Göran Andersson said:
As it's an implicit conversion, you can make it explicit if you want
without changing the resulting code:

Dim s As String = New String(c)

More to type, but it more clearly shows what's actally going on.

Well, I do not see any reason for that as widening conversions are not
harmful and always succeed. There's nothing wrong with implicit widening
conversions. I would not write 'Dim n As Long = CLng(<Integer>)' too.
Widening conversions are similar to the type/subtype relationship and I
assume you would not write 'Dim x As BaseClass = DirectCast(<DerivedClass>,
BaseClass)' in your code.
 
G

Göran Andersson

Herfried said:
Well, I do not see any reason for that as widening conversions are not
harmful and always succeed. There's nothing wrong with implicit
widening conversions. I would not write 'Dim n As Long =
CLng(<Integer>)' too. Widening conversions are similar to the
type/subtype relationship and I assume you would not write 'Dim x As
BaseClass = DirectCast(<DerivedClass>, BaseClass)' in your code.

The difference is that the conversion is creating a new object.
 
H

Herfried K. Wagner [MVP]

Göran Andersson said:
The difference is that the conversion is creating a new object.

Well, I know that, but I do not see any reason for using an explicit
conversion because VB provides implicit widening conversions (which cannot
even be turned off!).
 
G

Göran Andersson

Herfried said:
Well, I know that, but I do not see any reason for using an explicit
conversion because VB provides implicit widening conversions (which
cannot even be turned off!).

For readability. The code more closely resembles what's actually
happening, and perhaps also more closely resembles the intention of the
programmer.

I often use explicit conversions even if there might be an implicit
conversion. It's easier to maintain the code when it says exactly what's
happening.
 
C

Cor Ligthert[MVP]

Goran,
I often use explicit conversions even if there might be an implicit
conversion. It's easier to maintain the code when it says exactly what's
happening.

I knew bookkeepers who where checking every calculation done by a computer
by hand.

I don't know why your statement did remind me of this.

Cor
 

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