1

The variable datatype is string .it contain string value like greater than 300 chars. i want to split that string by 150 char and stored in the string array using vb.net

My code:

msg = t1("fld_msg")
msg1 = msg.Length
For i = 0 To msg.Length - 1
   strarr = msg.Substring(0, 150)
Next

Error:

value of type string cant be converted into one dimensional array
1
  • Can you give more code? Your code is blurry Commented Nov 5, 2012 at 10:15

3 Answers 3

1

You need a counter to increment the cells in the array

msg = t1("fld_msg")
msg1 = msg.Length
dim Counter as Integer = 0
For i = 0 To msg.Length - 1 Step 150
   strarr(Counter) = msg.Substring(i, 150)
   Counter += 1
Next
Sign up to request clarification or add additional context in comments.

3 Comments

Have you run your code for the string with length which is not a multiple of 150 ?
it does not have to be multiple of 150 for it to go through the loop. It can be 200 and it will still execute.
I didn't consider Step to restrict your code for multiple of 150, instead for example if msg has 315 characters, Do you think it will it will work? Since in 3rd iteration it will request 150 characters while it only have 15 characters left. My expectation will be an a raise of an exception for this case. May be i misunderstood your point.
1

Substring returns a value of type string.

You are trying to put the results into an array.

Try:-

strarr(0) = msg.Substring(0,150)
strarr(1) = msg.Substring(150)

Comments

0

Required correction in your code is to assign substring value to an index of array "strarr(i)" rather than to an array "strarr". Also taking a substring like (0,XX) is not correct. Every time it will return a substring from index 0, use (i*NumberOfCharactersToInclude,XX) instead. But here 'XX' also matters.

For example,

if string has 311 characters and XX is fixed to 150, it will lead to an error in third substring. So i would suggest you to go with this one: (Assuming Framework is 3.5 or above)

For i As Integer = 0 To len  ' len represents possible no. of substrings
    strarr(i) = New String(msg.Skip(i * 150).Take(150).ToArray)
Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.