473,671 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string.Format with strings as params?

Hi everybody,
I need simple string formatting like string.Format() does, but just with
strings what seems not to work.
Can anyone tell me please why this doesn't work and how can it be done? This
looked like the simple and good solution but no.

class Example
{
string format = "{1} {2} {3}"
string anotherFormat = "{3}{1}{2}" ;

public void Test()
{
Print(format);
Print(anotherFo rmat);
}

private void Print(string rightFormat)
{
// Here goes FormatException : Input string was not in a correct format
Console.WriteLi ne(string.Forma t(rightFormat, "one", "two", "three"));
}
}
Thank you in advance.
Dec 9 '06 #1
4 2323
Ok, as i tried to ilustrate the problem, I found my example working (+ wrong
inexes - should be from {0} of course), but this piece doesn't work for sure:

string.Format(" {0} {1} {2)", "one", "two", "three");

.... and i still wonder why :-/
Dec 9 '06 #2
Hi,

Elements of the format arguments array must be referenced in the format
string just like how an Array is accessed programmaticall y: with a
zero-based index.

Try "{0} {1} {2}" instead.

--
Dave Sexton

"carry" <ca***@discussi ons.microsoft.c omwrote in message
news:76******** *************** ***********@mic rosoft.com...
Hi everybody,
I need simple string formatting like string.Format() does, but just with
strings what seems not to work.
Can anyone tell me please why this doesn't work and how can it be done?
This
looked like the simple and good solution but no.

class Example
{
string format = "{1} {2} {3}"
string anotherFormat = "{3}{1}{2}" ;

public void Test()
{
Print(format);
Print(anotherFo rmat);
}

private void Print(string rightFormat)
{
// Here goes FormatException : Input string was not in a correct format
Console.WriteLi ne(string.Forma t(rightFormat, "one", "two", "three"));
}
}
Thank you in advance.

Dec 9 '06 #3
Count the pairs of { and }
"carry" <ca***@discussi ons.microsoft.c omwrote in message
news:FB******** *************** ***********@mic rosoft.com...
Ok, as i tried to ilustrate the problem, I found my example working (+
wrong
inexes - should be from {0} of course), but this piece doesn't work for
sure:

string.Format(" {0} {1} {2)", "one", "two", "three");

... and i still wonder why :-/

Dec 10 '06 #4
Hello carry,
>string.Format( "{0} {1} {2)", "one", "two", "three");
-----------------------------^
Because you have a closing paren in there instead of a closing curly. I
haven't tried to run it, but I can't imagine any other reason.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 11 '06 #5

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

Similar topics

4
9185
by: Alastair G. Hogge | last post by:
Hello *, I'm using Python and the cgi module to retrive data from a HTML form. I'm then trying to get that information into a string. But efforts fail when I run type(foo) on my string. type() returns tuple. My codes follows: #!/usr/local/bin/python import cgi
3
2979
by: Chris Mantoulidis | last post by:
I posted this here one day ago but it seems like it hasn't been put up for some unknown reason. That gives me a chance to say things a bit better in this post. 1st of all let's desribe the problem: In ParamGenerate() I want to find all whitespaces before a certain point in one string. Thus I use the string::find() function. However it seems like this function ignores some whitespaces :/ This is really weird and I need your help. Okay...
2
4037
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params object); What happens if i will use String.Format("{0} and {1}", "one", "two") instead of String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} and {1}", "one", "two")?
5
11149
by: sklett | last post by:
I have a simple debug and logging class and I would like to be able to send a non-formatted string to my various printing methods, much like string.format(). I have tried to implement this like so: public void PrintDebugError(string format, object parameters) { string buff = string.Format(format, parameters);
32
14838
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
485
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b & c & d & e +/- 420ms
4
2376
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript. Because I want to use some ajax-requests and other javascript-functions on my xhtml, I need to dynamically add event handlers to any possible dom-elements. All solutions I found so fare are for specific, pre-known
10
2615
by: John Vottero | last post by:
Why does String.Format have overrides of: Format(string, object) Format(string, object, object) Format(string, object, object, object) Format(string, params object) doesn't the last one cover the first three?
6
4733
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following format: struct mst_ {
0
8476
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
8914
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
8820
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
6223
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
5695
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();...
0
4224
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...
0
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
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...

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.