Hi,
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
character Position
012345678901234 567890123456789 012345678901234 567890123456789 012345678901234 567890123456789
FirstName LastName Address
street City st Zip xxx xxxxx xxxx
Thanks,
Mo 10 2811
Mo <le******@yahoo .comwrote:
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
Firstly, are you sure you actually need to make this faster? Is your
current code readable, and have you measured its performance? I would
only start going for more complicated options when you've got an issue.
Having said that, you could create a type which stores a char array and
allows you to replace portions of it easily - then create a new string
from that char array appropriately.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
On Mar 25, 1:04 pm, "Mo" <le_mo...@yahoo .comwrote:
Hi,
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
character Position
012345678901234 567890123456789 012345678901234 567890123456789 012345678901234 *56789012345678 9
FirstName LastName Address
street City st Zip xxx xxxxx xxxx
I would do something like this:
if (firstName.Leng th 10) firstName = firstName.Subst ring(0, 10);
....etc...
longString = String.Format(" {0,10}{1,10}... ", firstName,
lastName, ... );
This is the easiest to read. I would tune it for speed only in the
case that I were to benchmark it and find that it really is a
bottleneck in my code.
hi,
If I read your question correctly you have a series of Database
records which you are going to retrieve and turn into a text file
possibly for some proprietary system input.
My instinct on this one is:
Create a class
It would contain a series of strings.
One for each parameter.
Create an array of strings filled with spaces.
each array entry would contain the number of padding spaces that its
index represents.
ie ar[5] would be " " 5 spaces
In your data retrieval create and fill a series of these classes
Do the maths on the inbound strings and store the padded results.
the filled items into a collection say stack<T>
When writing to the file pop each item off and use its ToString() to
give you the finished line to write to file.
It is probably slower than the other suggestions, but you will find it
easy to maintain in 6 months when they change the file spec on you.
Use a stringbuilder in the ToString() override to easily concat the
whole lot.
or...
maybe I have been drinking too much coffee and this is overkill.
FWIW
Bob
Sort of like:
class stFixedString
{
string[] arSpace;
//Length constants
const int iGivenName = 20;
const int iSurname = 25;
public stFixedString()
{
arSpace = new string[5];
arSpace[0]="";
arSpace[1]=" ";
arSpace[2] = " ";
//etc
}
private string msGivenName;
public string GivenName
{
get { return msGivenName ; }
set
{
msGivenName = value + arSpace[iGivenName -
value.Length];
}
}
private string msSurname;
private int miSurnameLength ;
public string Surname
{
get { /*as above*/ }
set {/*as above*/ }
}
public override string ToString()
{
StringBuilder s = new StringBuilder() ;
s.Append(msGive nName);
s.Append(msSurn ame);
return s.ToString();
}
}
On 25 Mar 2007 13:04:04 -0700, "Mo" <le******@yahoo .comwrote:
>Hi,
I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasource and must substitute the values in the exact position insuring that paramteres end up exactly at position they need to be. I have this running now but I am looking for a smarter method. I am trying to build a string with 768 spaces to initalize. Then replace the spaces with characters one at a time to insure location of all parameters are preserved. I tried using stringbuilder but it does not have the functionality I am lookign for. Does Any body has a smart way of dong this?
character Position 01234567890123 456789012345678 901234567890123 456789012345678 901234567890123 456789012345678 9 FirstName LastName Address street City st Zip xxx xxxxx xxxx
Thanks, Mo
Mo wrote:
Hi,
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
character Position
012345678901234 567890123456789 012345678901234 567890123456789 012345678901234 567890123456789
FirstName LastName Address
street City st Zip xxx xxxxx xxxx
Thanks,
Mo
Use the StringBuilder to build the string from left to right, instead of
trying to creating a string and put things into it.
Example:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 40 - record.Length);
recoRd.Append(l astName);
record.Append(' ', 80 - recoed.Length);
....
string result = record.ToString ();
--
Göran Andersson
_____ http://www.guffa.com
On Mar 26, 2:20 am, Göran Andersson <g...@guffa.com wrote:
Mo wrote:
Hi,
I am trying to write a code to build a string 768 characters long.
This string is going to be written to a file which is then read by
another application. The format of the string is already defined. For
example Firstname starts at position 20, Last Name at position 30,
address at position 40 etc... I am reading these data from sql
datasource and must substitute the values in the exact position
insuring that paramteres end up exactly at position they need to be. I
have this running now but I am looking for a smarter method. I am
trying to build a string with 768 spaces to initalize. Then replace
the spaces with characters one at a time to insure location of all
parameters are preserved. I tried using stringbuilder but it does not
have the functionality I am lookign for. Does Any body has a smart way
of dong this?
character Position
012345678901234 567890123456789 012345678901234 567890123456789 012345678901234 *56789012345678 9
FirstName LastName Address
street City st Zip xxx xxxxx xxxx
Thanks,
Mo
Use the StringBuilder to build the string from left to right, instead of
trying to creating a string and put things into it.
Example:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 40 - record.Length);
recoRd.Append(l astName);
record.Append(' ', 80 - recoed.Length);
...
string result = record.ToString ();
This looked promising until I noticed the following.
1. StringBuilder.A ppend doesn't have an Append signature for (string,
int). It does, however, have (string, int, int), where the first int
is the start location in the string, so, Göran's code is easily
changed:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
....
string result = record.ToString ();
However, this still doesn't work, because if firstName or lastName is
longer than the maximum length, then the resulting record will be
misaligned, and the second int argument, the number of characters to
append, will be negative. The latter will cause Append to throw an
ArgumentOutOfRa nge exception.
So, you need to truncate the strings (if necessary), and _then_ do the
Append:
StringBuilder record = new StringBuilder() ;
if (firstName.Leng th 40) firstName = firstName.Subst ring(0, 40);
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
if (lastName.Lengt h 80) lastName = lastName.Substr ing(0, 80);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
....
string result = record.ToString ();
Bruce Wood wrote:
On Mar 26, 2:20 am, Göran Andersson <g...@guffa.com wrote:
>Mo wrote:
>>Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasource and must substitute the values in the exact position insuring that paramteres end up exactly at position they need to be. I have this running now but I am looking for a smarter method. I am trying to build a string with 768 spaces to initalize. Then replace the spaces with characters one at a time to insure location of all parameters are preserved. I tried using stringbuilder but it does not have the functionality I am lookign for. Does Any body has a smart way of dong this? character Position 0123456789012 345678901234567 890123456789012 345678901234567 890123456789012 34*567890123456 789 FirstName LastName Address street City st Zip xxx xxxxx xxxx Thanks, Mo
Use the StringBuilder to build the string from left to right, instead of trying to creating a string and put things into it.
Example:
StringBuilde r record = new StringBuilder() ; record.Append( firstName); record.Append( ' ', 40 - record.Length); recoRd.Append( lastName); record.Append( ' ', 80 - recoed.Length); ... string result = record.ToString ();
This looked promising until I noticed the following.
1. StringBuilder.A ppend doesn't have an Append signature for (string,
int).
Of course not, but it has an overload for Append(char, int), which is
what's used in the code.
Strings are delimited with quotes (").
Chars are delimited with apostrophes (').
It does, however, have (string, int, int), where the first int
is the start location in the string, so, Göran's code is easily
changed:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
...
string result = record.ToString ();
Nope. That doesn't even compile.
However, this still doesn't work, because if firstName or lastName is
longer than the maximum length, then the resulting record will be
misaligned, and the second int argument, the number of characters to
append, will be negative. The latter will cause Append to throw an
ArgumentOutOfRa nge exception.
So, you need to truncate the strings (if necessary), and _then_ do the
Append:
StringBuilder record = new StringBuilder() ;
if (firstName.Leng th 40) firstName = firstName.Subst ring(0, 40);
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
if (lastName.Lengt h 80) lastName = lastName.Substr ing(0, 80);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
...
string result = record.ToString ();
This is simpler, creates less strings and doesn't change the original
values:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName, 0, Math.Min(firstN ame.Length, 40));
record.Append(' ', 40 - record.Length);
record.Append(l astName, 0, Math.Min(lastNa me.Length, 40));
record.Append(' ', 80 - record.Length);
It also has the added benefit of working. ;)
--
Göran Andersson
_____ http://www.guffa.com
On Mar 26, 2:18 pm, Göran Andersson <g...@guffa.com wrote:
Bruce Wood wrote:
On Mar 26, 2:20 am, Göran Andersson <g...@guffa.com wrote:
Mo wrote: Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasource and must substitute the values in the exact position insuring that paramteres end up exactly at position they need to be. I have this running now but I am looking for a smarter method. I am trying to build a string with 768 spaces to initalize. Then replace the spaces with characters one at a time to insure location of all parameters are preserved. I tried using stringbuilder but it does not have the functionality I am lookign for. Does Any body has a smart way of dong this? character Position 01234567890123 456789012345678 901234567890123 456789012345678 901234567890123 4**567890123456 789 FirstName LastName Address street City st Zip xxx xxxxx xxxx Thanks, Mo
Use the StringBuilder to build the string from left to right, instead of
trying to creating a string and put things into it.
Example:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 40 - record.Length);
recoRd.Append(l astName);
record.Append(' ', 80 - recoed.Length);
...
string result = record.ToString ();
This looked promising until I noticed the following.
1. StringBuilder.A ppend doesn't have an Append signature for (string,
int).
Of course not, but it has an overload for Append(char, int), which is
what's used in the code.
Strings are delimited with quotes (").
Chars are delimited with apostrophes (').
Ouch. Need to reduce the resolution of my monitor, obviously. Didn't
notice the difference between ' and " in your code. Sorry.
It does, however, have (string, int, int), where the first int
is the start location in the string, so, Göran's code is easily
changed:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
...
string result = record.ToString ();
Nope. That doesn't even compile.
No, of course it wouldn't: I cut-and-pasted your code, assuming that
the char ' ' was in fact a string " ".
However, this still doesn't work, because if firstName or lastName is
longer than the maximum length, then the resulting record will be
misaligned, and the second int argument, the number of characters to
append, will be negative. The latter will cause Append to throw an
ArgumentOutOfRa nge exception.
So, you need to truncate the strings (if necessary), and _then_ do the
Append:
StringBuilder record = new StringBuilder() ;
if (firstName.Leng th 40) firstName = firstName.Subst ring(0, 40);
record.Append(f irstName);
record.Append(' ', 0, 40 - record.Length);
if (lastName.Lengt h 80) lastName = lastName.Substr ing(0, 80);
record.Append(l astName);
record.Append(' ', 0, 80 - record.Length);
...
string result = record.ToString ();
This is simpler, creates less strings and doesn't change the original
values:
StringBuilder record = new StringBuilder() ;
record.Append(f irstName, 0, Math.Min(firstN ame.Length, 40));
record.Append(' ', 40 - record.Length);
record.Append(l astName, 0, Math.Min(lastNa me.Length, 40));
record.Append(' ', 80 - record.Length);
It also has the added benefit of working. ;)
I know it's nit-picky, but your code and my code create the same
number of strings, I believe. The benefit of yours is that, as you
stated, it doesn't alter firstName or lastName.
Bruce Wood wrote:
On Mar 26, 2:18 pm, Göran Andersson <g...@guffa.com wrote:
>Bruce Wood wrote:
>>On Mar 26, 2:20 am, Göran Andersson <g...@guffa.com wrote: Mo wrote: Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasourc e and must substitute the values in the exact position insuring that paramteres end up exactly at position they need to be. I have this running now but I am looking for a smarter method. I am trying to build a string with 768 spaces to initalize. Then replace the spaces with characters one at a time to insure location of all parameter s are preserved. I tried using stringbuilder but it does not have the functionality I am lookign for. Does Any body has a smart way of dong this? character Position 01234567890 123456789012345 678901234567890 123456789012345 678901234567890 1234**567890123 456789 FirstName LastName Address street City st Zip xxx xxxxx xxxx Thanks, Mo Use the StringBuilder to build the string from left to right, instead of trying to creating a string and put things into it. Example: StringBuilde r record = new StringBuilder() ; record.Appen d(firstName); record.Appen d(' ', 40 - record.Length); recoRd.Appen d(lastName); record.Appen d(' ', 80 - recoed.Length); ... string result = record.ToString (); This looked promising until I noticed the following. 1. StringBuilder.A ppend doesn't have an Append signature for (string, int).
Of course not, but it has an overload for Append(char, int), which is what's used in the code.
Strings are delimited with quotes ("). Chars are delimited with apostrophes (').
Ouch. Need to reduce the resolution of my monitor, obviously. Didn't
notice the difference between ' and " in your code. Sorry.
>>It does, however, have (string, int, int), where the first int is the start location in the string, so, Göran's code is easily changed: StringBuild er record = new StringBuilder() ; record.Append (firstName); record.Append (' ', 0, 40 - record.Length); record.Append (lastName); record.Append (' ', 0, 80 - record.Length); ... string result = record.ToString ();
Nope. That doesn't even compile.
No, of course it wouldn't: I cut-and-pasted your code, assuming that
the char ' ' was in fact a string " ".
>>However, this still doesn't work, because if firstName or lastName is longer than the maximum length, then the resulting record will be misaligned, and the second int argument, the number of characters to append, will be negative. The latter will cause Append to throw an ArgumentOutOf Range exception. So, you need to truncate the strings (if necessary), and _then_ do the Append: StringBuild er record = new StringBuilder() ; if (firstName.Leng th 40) firstName = firstName.Subst ring(0, 40); record.Append (firstName); record.Append (' ', 0, 40 - record.Length); if (lastName.Lengt h 80) lastName = lastName.Substr ing(0, 80); record.Append (lastName); record.Append (' ', 0, 80 - record.Length); ... string result = record.ToString ();
This is simpler, creates less strings and doesn't change the original values:
StringBuilde r record = new StringBuilder() ; record.Append( firstName, 0, Math.Min(firstN ame.Length, 40)); record.Append( ' ', 40 - record.Length); record.Append( lastName, 0, Math.Min(lastNa me.Length, 40)); record.Append( ' ', 80 - record.Length);
It also has the added benefit of working. ;)
I know it's nit-picky, but your code and my code create the same
number of strings, I believe. The benefit of yours is that, as you
stated, it doesn't alter firstName or lastName.
Nope, my code creates a smaller (or sometimes equal) number of strings. :)
When you use Substring to truncate a string, it creates another string.
Instead I use the overload of the Append method that appends a part of a
string. This does not create an extra truncated string, but reads
directly from the original string.
The difference in performance is of course negligable in this case.
Creating another string hardly ever makes a difference, at least not
when they are as small as here, and as few. :)
--
Göran Andersson
_____ http://www.guffa.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew Palumbo |
last post by:
Hi,
I have a question about defining long string constants in class
defintions.
For an example, let's say I have some long SQL queries that I'd like
to store in a class. Furthermore, I don't want to instantiate the
class to use them since they're just a group of constants.
I can see a couple ways to acheive this, but they all leave...
|
by: D. Alvarado |
last post by:
Hi, I have this arr
$months = array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December");
and I would like to take this array and build a string of the form
"<option value=\"1\">January</option>\n<option
value=\"2\">February</option>\n<option...
|
by: Yuriy |
last post by:
Hi,
Can anybody explain the following?
Say I have the following source XML and XSLT (see below). No matter what
this XSLT does. It is just a sample to show a problem. the idea is that
XSLT transforms small XML into quite big XML. Now, I have a straightforward
C# (see below) code that does this transform and writes result into...
|
by: Anony |
last post by:
Hi All,
I'm trying to chunk a long string SourceString into lines of LineLength
using this code:
Dim sReturn As String = ""
Dim iPos As Integer = 0
Do Until iPos >= SourceString.Length - LineLength
sReturn += SourceString.Substring(iPos, LineLength) + vbCrLf
iPos += LineLength
|
by: John Williams |
last post by:
How can I get the build configuration string (Debug or Release) at run
time?
I currently use the following code to get the version number from the
AssemblyInfo.vb AssemblyVersion attribute, and wonder if there is
something similar for getting the build configuration.
Private Function getAppVersion() As String
Dim aName As...
| |
by: Henrik |
last post by:
The problem is (using MS Access 2003) I am unable to retrieve long
strings (255 chars) from calculated fields through a recordset.
The data takes the trip in three phases:
1. A custom public function returns a long string. This works.
2. A query has a calculated field based on the custom function above.
This works when the query is run...
|
by: Mike TI |
last post by:
Nov 13, 2007
Hi all
Can I build a string for arithmetic operation. (VB NET 2005)
e.g.
I want to build an arithmetic operation from within the program as
|
by: huohaodian |
last post by:
Hi,
How can I define a long string variable in C# with multiple lines ?
For example
private string longName = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
by: John |
last post by:
Hi
I have written a function to split a string into sub strings of a given
fixed max length. This is useful for example in breaking a long message into
multiple strings of up to 160 characters to be sent as individual SMS.
I am posting the function here in case its useful for someone.
Regards
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |