Concatenating Chars in an Array

  • Thread starter Thread starter eBob.com
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
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.
 
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
 
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.

:)
 
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
 
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.
 
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
 
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.
 
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.
 
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!).
 
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.
 
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
 
Back
Top