0

Why does the following code print 'foo' Length: 10000?

How do I discard the unused array elements such that the string is "Length: 3"?

Dim arr(9999) as char

Dim sw As StreamWriter = New StreamWriter("example.txt")
sw.WriteLine("foo")
sw.Close()

Dim sr As StreamReader = New StreamReader("example.txt")
sr.ReadBlock(arr, 0, 10000)
sr.Close()

Dim con as string = New String(arr)
Console.WriteLine(String.Format("'{0}' Length: {1}", con, con.Length.ToString()))
Dan Bechard
  • 5,104
  • 3
  • 34
  • 51

2 Answers2

3

You can use Substring:

Dim newString As String = con.Substring(0, 3)
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • What do I pass for the second argument if I don't know how long the contents of the array are? – Dan Bechard Nov 10 '15 at 15:56
  • @Dan: What do you mean you don't know? You specified constant values for 3 characters. In this case your answer is 3. Please give another example then. – Victor Zakharov Nov 10 '15 at 15:57
  • It's a file buffer: `oFile.ReadBlock(arrFileBuffer, 0, 16000)`. If the file only contains 12 characters, how do I get a string that is Length 12, rather than Length 16000? I can't use `oFile.ReadToEnd()` because sometimes the file is 40000 characters, in which case I only want to read the first 16000. – Dan Bechard Nov 10 '15 at 15:58
  • 1
    @Dan: [From MSDN](https://msdn.microsoft.com/en-us/library/system.io.streamreader.readblock%28v=vs.110%29.aspx): **Return Value.** `The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.` Just save results of ReadBlock call and use that to pass into Substring's second argument. – Victor Zakharov Nov 10 '15 at 16:01
  • I overlooked that. Awesome, thanks! – Dan Bechard Nov 10 '15 at 16:02
2

not sure why this works, but....

    Dim lChar As New List(Of Char)
    lChar.Add("f")
    lChar.Add("o")
    lChar.Add("o")
    Dim con2 As String = lChar.ToArray ' ?????
    Debug.Print("'" & con2 & "' Length: " & con2.Length)

I guess a String is really an array at heart.

rheitzman
  • 2,247
  • 3
  • 20
  • 36
  • You're starting off with a list of length 3, whereas I have a char array of length 10. Not sure how this answer is relevant to the question. – Dan Bechard Nov 11 '15 at 14:03
  • The sample code uses a List which grows dynamically with each .Add. the answer isn't directly relevant but shows an alternate method to avoid pre-dimensioned arrays. – rheitzman Nov 12 '15 at 18:49
  • In the context of my problem, a fixed-dimension array is required, hence the question using that type specifically. I understand there are many other ways to represent a collection of characters, but none of them are relevant to this particular question unless you can efficiently convert an array to the type being used (I was trying to avoid unnecessary memory copies, because in reality the array is a large file not a 3-character string). – Dan Bechard Nov 13 '15 at 17:26
  • I see that now - I suggest you include the level of detail provided in your comment: "Its a file buffer" in the original post. BTW you can go back and edit the OP to add more information. – rheitzman Nov 13 '15 at 19:19
  • I did that, a few hours before your comment. Originally, I was trying to generalize the problem a bit too much. Thanks for the answer and tip! – Dan Bechard Nov 14 '15 at 00:43