views:

75

answers:

5

I have a little chunk of code (see below) that is returning the string:

string.Format("{0}----{1}",3,"test 2");

so how do I get this to actually "Execute"? To run and do the format/replacement of {0} and {1}?

My Code snippet:

StringBuilder sb = new StringBuilder();
sb.Append("{0}----{1}\",");
sb.AppendFormat(ReturnParamValue(siDTO, "siDTO.SuggestionItemID,siDTO.Title"));
string sbStr = "=string.Format(\""+sb.ToString()+");";

yes, ReturnParamValue gives the actually value of the DTO.

Anyways, I've taken a look at the following (but it doesn't say how to execute it: http://stackoverflow.com/questions/3319896/how-to-get-string-format-not-to-parse-0

Maybe, I just should put my code snippet in a method. But, what then?

A: 

I don't think you can execute it. Java is not really a interpreted language.

You may make use of scripting languages (which can even embed in your Java app as I know, start from JDK6) for such purpose, like Groovy

Adrian Shum
A: 

You could use RegEx to parse the three parameters out of the string, and then pass them to a real, actual string.Format method :-)

LesterDove
That aside, I think the thing you're looking for is an "expression evaluator" so you could Google for that, once the aforementioned language debate is settled...
LesterDove
A: 

It looks like what you want is something like this:

string sbStr = string.Format("{0}----{1}", siDTO.SuggestionItemID, siDTO.Title);
Gabe
+1  A: 

Why are you including String.Format in the string itself?

If you're looking for a generic "let me evaluate this arbitrary expression I've built up in a string" then there isn't a simple answer.

If, instead, you're looking at how to provide the parameters to the string from a function call, then you've got yourself all twisted up and working too hard.

Try something like this, based on your original code:

string result
    = string.Format(
        "{0}----{1}",
        ReturnParamValue(siDTO, "siDTO.SuggestionItemID,siDTO.Title"));

Though, this won't entirely work since your original code seems to be only providing a single value, and you have two values in your format string - the {0} will be replaced with the value from your function, and {1} left unchanged.

What output are you expecting?

Does your ReturnParamValue() function try to return both the label and the value in a single string? If it does, and if they're comma separated, then you could try this:

var value = ReturnParamValue(siDTO, "siDTO.SuggestionItemID,siDTO.Title"));
var pieces = string.Split(',');
string result
    = string.Format( "{0}----{1}", pieces[0], pieces[1]);

Though this is seriously working too hard if ReturnParamValue() is a method you control.

Update Fri 6 August

Check out the declaration for string.Format() as shown on MSDN:

public static string Format(
    string format,
    params Object[] args
)

Unlike the special casing you might have seen in C for printf(), there's nothing special or unusual about the way string.Format() handles multiple parameters. The key is the params keyword, which asks the compiler to provide a little "syntactic sugar" where it combines the parameters into an array for you.

Key here is that the wrapping doesn't happen if you're already passing a single object[] - so if you wanted to, you could do something like this:

object[] parameters 
    = ReturnParamValues(siDTO, "siDTO.SuggestionItemID,siDTO.Title");
string result
    = string.Format("{0}----{1}----{2}", parameters);

Though, if I saw something like this in any codebase I maintained, I'd be treating it as a code-smell and looking for a better way to solve the problem.

Just because it's possible doesn't mean it's advisable. YMMV, of course.

Bevan
gets me thinking
Patrick From An IBank
the 1st didn't work
Patrick From An IBank
as for the 2nd - BINGO!!! The only question: Is there a way to make pieces[0], pieces[1] dynamic? Hence, I don't ned to make a code change when there are 3 pieces? Personally, I don't think so.
Patrick From An IBank
Bevan - THanks for all your help. Talked over with the boss and I'm going to try the following: (1) Use Reflection to get the Values in Parameter (2) Put the params in an array (3) Loop through the params and replace the string with the value that I got in step 1. Keep your fingers crossed!
Patrick From An IBank
A: 

Maybe i didn't understand your question completely, but it sounds like you need to format a format-string. If that's true you could maybe try something like this:

int width = 5;
string format = String.Format("{{0,{0}}}----{{1,{0}}}", width);
string result = String.Format(format, "ab", "cd");

So the trick is simply to escape the { or } by using a double {{ or }}.

Oliver