473,407 Members | 2,326 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,407 software developers and data experts.

Easy way to get a formatted string

I am capturing a set of validation messages from my validation method. I
was putting each message back into a StringCollection. I then wanted to
output a big warning message in a Dialog box. Is there a way to quickly
convert essentially an ArrayList of strings into a single string with each
element separated with a newline?

or is

foreach(string s in StrList) newstring.concat("\n" + s);

what I need to do?

thx
Nov 16 '05 #1
6 1148
oj
Try:

string result = String.Join("\n",StrList);
--
-oj
"Jack Addington" <ja********@shaw.ca> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
I am capturing a set of validation messages from my validation method. I
was putting each message back into a StringCollection. I then wanted to
output a big warning message in a Dialog box. Is there a way to quickly
convert essentially an ArrayList of strings into a single string with each
element separated with a newline?

or is

foreach(string s in StrList) newstring.concat("\n" + s);

what I need to do?

thx

Nov 16 '05 #2
I would use a string builder to add messages to it, and add "\r\n" to
each message string. Something like this:

public void SaveData ()
{
StringBuilder errMsgs = new StringBuilder ();
if (!ValidateData (errMsgs))
MessageBox.Show (errMsgs.ToString);
else
{
//Save stuff here
}
}

public bool ValidateData (StringBuilder errMsgs)
{
bool isValid = true;
if (fnameText.TextLength == 0)
{
errMsgs.Append ("Please enter a valid first name \r\n");
isValid = false;
}
if (lnameText.TextLength == 0)
{
errMsgs.Append ("Please enter a valid last name \r\n");
isValid = false;
}
return isValid;
}

NuTcAsE

Nov 16 '05 #3
I did this by inheriting from StringCollection and then overriding
ToString().

--Bob

"Jack Addington" <ja********@shaw.ca> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
I am capturing a set of validation messages from my validation method. I
was putting each message back into a StringCollection. I then wanted to
output a big warning message in a Dialog box. Is there a way to quickly
convert essentially an ArrayList of strings into a single string with each
element separated with a newline?

or is

foreach(string s in StrList) newstring.concat("\n" + s);

what I need to do?

thx

Nov 16 '05 #4
I think oj's solution is the best option here. In this situation, we
have all the strings that need to be joined, so a call to string.Join(.
.. .) would be the simplest. Using StringBuilder would complicate the
code and yield no benefit. NuTcAsE, if you think I'm wrong, please do
a test and post the results.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #5
thank you all for the quick replies

"Jeffrey Palermo, MCAD.Net" <je************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I think oj's solution is the best option here. In this situation, we
have all the strings that need to be joined, so a call to string.Join(.
. .) would be the simplest. Using StringBuilder would complicate the
code and yield no benefit. NuTcAsE, if you think I'm wrong, please do
a test and post the results.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #6
Jack,

Glad we could help.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #7

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

Similar topics

33
by: Jim Hill | last post by:
I've done some Googling around on this and it seems like creating a here document is a bit tricky with Python. Trivial via triple-quoted strings if there's no need for variable interpolation but...
4
by: Fabian | last post by:
I have a three tier nested array, used to define a map for a javascript game, and can be edited within the web page. Is there a way I can generate a visible copy of this array that I can then c&p...
2
by: JSheble | last post by:
I have a method in my class that needs to return formatted XML, with the carriage returns, linefeeds, and tabs... However, when I return oXml.OuterXml, the Xml is not formatted... Every example...
1
by: Alex | last post by:
Hi I'm wondering if there is an easy way to analize log files in php? I'm sure there is a program somethere on the internet. If anyone can point me to one it would be great. I have a log file...
1
by: intrepid_dw | last post by:
Hello, all. I have a Windows Forms app with a datagrid control, with several custom column styles applied. One of the columns holds a decimal data type, with a format string of "C" for currency,...
2
by: oyster | last post by:
I find that the existing email moudle is some hard for me to understand, especially the part of how to set the CC, BCC and attach the files. Is there any more easy one like this p-code? import...
9
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and...
3
by: Matt Brown - identify | last post by:
Hello, I'm trying to figure out a method to look up by a range of dates, entries in a database. The format of the date in the database is "M\D \yyyy HH:MM:SS". What i need to do is take the...
2
by: Artie | last post by:
Hi, I've searched the web but can't find a solution to an apparently really simple problem. My app contains an HTML string and I need to be able to invoke the Print Dialog to print the HTML...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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,...
0
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...

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.