473,396 Members | 1,712 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.

Split String by number of characters

Hi,

I want to split a big string by nymber of characters. for eg., if total
length of the string is 80 and I want to split by each 10 characters and get
8 lines of output.

I written a function but still I search for the optimized one because the
strings I receive will be long. Is there are any special classes or I can use
regular expressions?

Please help.

TIA,
Holy

Nov 22 '05 #1
2 10361
> I want to split a big string by nymber of characters. for eg., if total
length of the string is 80 and I want to split by each 10 characters and get
8 lines of output.

I written a function but still I search for the optimized one because the
strings I receive will be long. Is there are any special classes or I can use
regular expressions?


I don't believe there are any classes designed specifically for this.
Just don't code the function as

while (Input != "")
{
int ThisLength = Math.Max(TargetLength, Input.Length);
Output.Add(Input.Substring(0, ThisLength));
Input = Input.Remove(0, ThisLength);
// all this 'popping' is VERY expensive on big strings
}

instead, move a LeftPtr along, and Substring() from that.

Alternatively, you could construct a regex

string Pattern = String.Format(".{1,{{0}}}", TargetLength)
// or
string Pattern = ".{1," + TargetLength.ToString() + "}";

// in either case, be sure to use RegexOptions.Singleline

and construct your output from the Matches collection (or by calling
Match / NextMatch). Might be a bit less code than the LeftPtr
alternative, and probably about as performant.

--

www.midnightbeach.com
Nov 22 '05 #2
Holysmoke,
As Jon suggests there are no built-in functions per se.

I would consider using a For loop & String.SubString (variation of Jon's
LeftPtr), something like:

Dim length As Integer = 4
Dim input As String = "123456789012"

Dim lines((input.Length \ length) - 1) As String

For index As Integer = 0 To lines.Length - 1
lines(index) = input.Substring(index * length, length)
Next

NOTE: This function assumes that the String.Length is a multiple of the
length parameter, any extra is ignored...

Hope this helps
Jay

"Holysmoke" <Ho*******@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com...
Hi,

I want to split a big string by nymber of characters. for eg., if total
length of the string is 80 and I want to split by each 10 characters and
get
8 lines of output.

I written a function but still I search for the optimized one because the
strings I receive will be long. Is there are any special classes or I can
use
regular expressions?

Please help.

TIA,
Holy

Nov 22 '05 #3

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

Similar topics

2
by: Holysmoke | last post by:
Hi, I want to split a big string by nymber of characters. for eg., if total length of the string is 80 and I want to split by each 10 characters and get 8 lines of output. I written a...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
8
by: Wijaya Edward | last post by:
Hi all, I was trying to split a string that represent chinese characters below: ??? But why the split function here doesn't seem
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
14
by: tom t/LA | last post by:
Here is a function to convert a CSV file to a Javascript array. Uses idealized file reading functions based on the std C library, since there is no Javascript standard. Not fully tested. ...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.