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
Mar 25 '07
10 2817
On Mar 26, 6:39 pm, Göran Andersson <g...@guffa.com wrote:
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 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 012345678901 234567890123456 789012345678901 234567890123456 789012345678901 234***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: StringBuild er 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 (').
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: StringBuilde r 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 ArgumentOutOfR ange exception. So, you need to truncate the strings (if necessary), and _then_ do the Append: StringBuilde r 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:
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.
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.
Right you are. 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 unreadable
|
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 value=\"3\">March</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 XmlTextWriter.
(Oleg's NXSLT.EXE is also suitable).
|
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 System.Reflection.AssemblyName
| |
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 directly.
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |