473,385 Members | 1,983 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,385 software developers and data experts.

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(anotherFormat);
}

private void Print(string rightFormat)
{
// Here goes FormatException: Input string was not in a correct format
Console.WriteLine(string.Format(rightFormat, "one", "two", "three"));
}
}
Thank you in advance.
Dec 9 '06 #1
4 2309
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 programmatically: with a
zero-based index.

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

--
Dave Sexton

"carry" <ca***@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.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(anotherFormat);
}

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

Dec 9 '06 #3
Count the pairs of { and }
"carry" <ca***@discussions.microsoft.comwrote in message
news:FB**********************************@microsof t.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
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()...
3
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...
2
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...
5
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...
32
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...
3
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...
4
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....
10
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...
6
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.