473,786 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Appending a string

Hiya

I have a string with about 300+ characters, how can i insert a line break or
another character every 50 characters for the whole string?

Paul Roberts
Dec 9 '05 #1
18 2914
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.New Line and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@c ompserve.com> wrote in message
news:dn******** ***********@new s.demon.co.uk.. .
Hiya

I have a string with about 300+ characters, how can i insert a line break
or another character every 50 characters for the whole string?

Paul Roberts

Dec 9 '05 #2
string x = "your long string";

for (int i = 50; i < x.Length; i += 50)
x = x.Insert(i, System.Enviornm ent.NewLine);

Dec 9 '05 #3
Hi,

"Steve" <st**********@t icketmaster.com > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
string x = "your long string";

for (int i = 50; i < x.Length; i += 50)
x = x.Insert(i, System.Enviornm ent.NewLine);


It's better to use StringBuilder, with the above code you are generating a
lot of temp strings that consume resources.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 9 '05 #4
This is not very efficient since a new String object is created in each
loop.

"Steve" <st**********@t icketmaster.com > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
string x = "your long string";

for (int i = 50; i < x.Length; i += 50)
x = x.Insert(i, System.Enviornm ent.NewLine);

Dec 9 '05 #5
Rather than splitting the string, he could just initialize a new
StringBuilder with that string and use StringBuilder.I nsert to add the
characters. I think this will push the string.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:e$******** ******@tk2msftn gp13.phx.gbl...
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.New Line and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@c ompserve.com> wrote in message
news:dn******** ***********@new s.demon.co.uk.. .
Hiya

I have a string with about 300+ characters, how can i insert a line break
or another character every 50 characters for the whole string?

Paul Roberts


Dec 9 '05 #6
If the string is, in fact, very long (thousands of characters) this
will result in a lot of unnecessary copying.

You're better off breaking up the string first, then gluing it back
together with newlines in between using StringBuilder.

Dec 9 '05 #7
Hi,
"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:ex******** ********@TK2MSF TNGP14.phx.gbl. ..
Rather than splitting the string, he could just initialize a new
StringBuilder with that string and use StringBuilder.I nsert to add the
characters. I think this will push the string.


I think the same but I have no idea if that can be more efficient that
using Append , note that there is a version of Append that receive a string
and two integers and append that substring:
Append( string, start_index, end_index )

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 9 '05 #8
Can someone give me a head start? i'm having problems with it, and dont know
where to start.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:e$******** ******@tk2msftn gp13.phx.gbl...
Hi,

Basically, you cannot, String is an inmutable class.
What you can do is split the string in pieces, append the piece to a
StringBuilder, append Environment.New Line and repite the process.

I will let the coding to you, it's the fun part :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Paul Roberts" <Pa**********@c ompserve.com> wrote in message
news:dn******** ***********@new s.demon.co.uk.. .
Hiya

I have a string with about 300+ characters, how can i insert a line break
or another character every 50 characters for the whole string?

Paul Roberts


Dec 9 '05 #9
Here are some hints.

Look at the documentation for the StringBuilder class. Notice that you
can create an empty StringBuilder and append things to it, then convert
the finished product to a string using the ToString() method of your
StringBuilder object.

If you really want every exactly 50 characters, just check into the
Substring method of String. If you need to do a more sophisticated
determination of where to break the string, write a method that takes a
string and an index and returns the length of the substring to break
off. For example, if you wanted to break the string at a blank space,
but at most 50 characters, a method like this might be useful:

private static int LineLength(stri ng line, int startIndex)
{
int endIndex = startIndex + 50;
if (endIndex >= line.Length)
{
endIndex = line.Length - 1;
}
while (endIndex > startIndex && !Char.IsWhiteSp ace(line[endIndex]))
{
endIndex--;
}
if (endIndex <= startIndex)
{
// return 50 or the remainder of the string, whichever is less
return Math.Min(50, line.Length - startIndex);
}
else
{
return endIndex - startIndex;
}
}

The part where you break up the string and reassemble it with newlines
would look like this:

int startIndex = 0;
StringBuilder sb = new StringBuilder() ;
while (startIndex < line.Length)
{
if (sb.Length > 0)
{
... append a newline to the StringBuilder ...
}
... append a chunk of the string "line" to the StringBuilder
... update "startIndex " to be the next place in the string "line"
to start
}
.... convert the StringBuilder to a string

and you're done!

Dec 9 '05 #10

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

Similar topics

2
1610
by: Robizzle | last post by:
I am having problems appending a number to the end of a string. I searched google and google forums and couldn't figure it out .... sorry i'm sure its simple. so I have: $filename = 'test.file."; $i = 0; and I want to change $filename to become 'test.file.0'.
16
12641
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. I have tried and tried but just cant seem to get this right, I am using Queries to created my export files with specifications which works fine, I get stumped with the appending the header to my transaction file. What I have so far looks like...
3
1930
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending many records. Two of the target fields in tblCorrespondence receiving values in the append operation are and . For any given VehicleJobID value, I want only ONE record in correspondence table to have an value of "01". This query blindly appends...
1
2716
by: mikemac76 | last post by:
I am trying to build a test harness to practice appending strings to an rtf string. I am doing this to simulate the string I'll be sending over the wire and receiving on the other end of an IM application. Below is the code I use to test. I receive this error everytime I try to send the string from one rtb to another: "File Format is not valid Line 28" Line 28 is where I set rtb2 to the value of the newly concatanated...
2
2243
by: tony.collings | last post by:
I started a thread here : http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6 About creating an Email function to email authors when the Review Date has expired on their page. I've managed to now achieve a significant proportion of the work by writing out the details to an XML file and reading them back. However I'm a little stuck on Amending...
4
1671
by: John A Grandy | last post by:
could someone explain the following to me : Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier forces it to Integer.
3
565
by: Jim | last post by:
Could anyone please point me towards some code that allows me to add to an existing XML file using the output of an HTML form. I want to add a form on my website so users can input their email address in a text field and feedback in a text area and then submit it. I can output the data no probs but appending it first to the XML file is proving tricky. Any ideas?
1
1565
by: libsfan01 | last post by:
hello again! another problem im having is appending a value to a string. how come this function isnt working for me? string overwrites the value of content not gets added onto the end of it?! function alertchange(value) { var string = value;
7
9508
by: Daz | last post by:
Hi everyone! Is it possible to take a line of text like so: <tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr> And append it to a DOM node such as this: var nodeToAppendTo = document.getElementById('tbody');
1
3884
by: KiddoGuy | last post by:
I am trying to build a string character by character. However, when I use the overloaded += or string.append() method, the character replaces the one before it rather than appending to it so I am constantly left with a string of length 1. Here's a sample of what I'm trying to do. char cur; while(!fin.eof()) { //(fin is an ofstream) string currstr = ""; //This is part of what's giving me a problem /* build strings from...
0
9647
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, 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...
0
9491
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,...
0
10357
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, 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...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.