473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Separate a string

Hello, I would like to know how to split a string variable in x
lines(possibly an array) of a maximum length of 30 chrs, but without
truncating words.

I Think there's a trick to do with substring, but don't know much.

Thanks to help me on this issue,

Sebastien
Nov 21 '05 #1
3 1538
dim arrString() as string

'this code splits your string in a array, word by word, separated by space:
arrString = "your string".split(" ")
If you need your string be approximated to 30 chars, you can do some loops
and concatenate the array elements on a second array. For sample:

Dim i As Integer
Dim strLine As String
Dim arrString2() As String
Dim intUboundStr2 As Integer = -1

'transfer all strings from first array, to second array,
'trying to limite each line till 30 characters
For i = 0 To arrString.Length - 1
If Len(strLine & arrString(i)) <= 30 Then
strLine = strLine & " " & arrString(i)
Else
intUboundStr2 = intUboundStr2 + 1
ReDim Preserve arrString2(intUboundStr2)
arrString2(intUboundStr2) = strLine
strLine = ""
End If
Next
'transfer the eventual remanescent characters
If strLine <> "" Then
intUboundStr2 = intUboundStr2 + 1
ReDim Preserve arrString2(intUboundStr2)
arrString2(intUboundStr2) = strLine
End If
End Sub
Note: you can use StringBuilder class to eventually help your code speed up,
incase you are working with large strings.
[]s
Cesar


"Ze Coder" <ze*****@nospam.com> escreveu na mensagem
news:ev**************@TK2MSFTNGP15.phx.gbl...
Hello, I would like to know how to split a string variable in x
lines(possibly an array) of a maximum length of 30 chrs, but without
truncating words.

I Think there's a trick to do with substring, but don't know much.

Thanks to help me on this issue,

Sebastien


Nov 21 '05 #2
Ze,

Another method than Ronchese avoiding the redim which is eating resources in
his sample.
(Ronchese uses a classic VB approach, with what is nothing wrong except than
that Redim is not so lucky).

It is typed in this message and not tested so watch typos or whatever

\\\
Dim startpoint as integer
dim stringlength as integer = mystring.length
dim count as integer = stringlength \ 30
dim ar as new arraylist
For i as integer = 0 to count
ar.add(mystring.substring(startpoint,30))
startpoint =+ 30
next
ar.add(mystring.substring(startpoint)
///

I hope this helps,

Cor
Nov 21 '05 #3
Thanks to both of you!
"Cor Ligthert [MVP]" <no************@planet.nl> a écrit dans le message de
news: #4**************@TK2MSFTNGP12.phx.gbl...
Ze,

Another method than Ronchese avoiding the redim which is eating resources in his sample.
(Ronchese uses a classic VB approach, with what is nothing wrong except than that Redim is not so lucky).

It is typed in this message and not tested so watch typos or whatever

\\\
Dim startpoint as integer
dim stringlength as integer = mystring.length
dim count as integer = stringlength \ 30
dim ar as new arraylist
For i as integer = 0 to count
ar.add(mystring.substring(startpoint,30))
startpoint =+ 30
next
ar.add(mystring.substring(startpoint)
///

I hope this helps,

Cor

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jaime Wyant | last post by:
I know I've seen this somewhere, but can't seem to google it. Is there a way to use an alternate statement separator, other than the default ';'? jw
2
by: Stian | last post by:
Hi! I have made a form with two SELECT LIST-boxes where the first is dynmically filled when you enter the page, and then one can select multiple options from the list and move across to the...
3
by: Roberto Becerril | last post by:
Hi forum, i'm a little new in javascript and maybe you can help me. I have an html form and an icon, if i click on the icon, a new pop-up window is open and shows a list of numbers with a...
3
by: harhaiko | last post by:
Hello, I have 4,4 million rows of data in one column. I want to search for a string in each line. There are random amount of characters before the string I want to find. The string starts with the...
2
by: ad | last post by:
I have a string vaiable like "1a3345"; How to separate the string into a char array or a string array just have on character per element?
28
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I...
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
1
by: drec | last post by:
I have a very simple mysql/php script that is being used to store program keys in a db for multiple users. I would like to add the ability to send these string values to another application. In...
2
by: colleen1980 | last post by:
Hi: Can any one please tell me how do i separate city state and zip. I ask this question in previous post. But when the city name is in two words it mess up my values otherwise it works. ...
14
by: Lambda | last post by:
I'd like to create separate character pointers, pass them to a function to assign each one different value, and assign the pointers to an array. But when I try: #include <stdio.h> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.