Reverse of String.Format? [duplicate] - c#

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Parsing formatted string.
How can I use a String.Format format and transform its output to its inputs?
For example:
string formatString = "My name is {0}. I have {1} cow(s).";
string s = String.Format(formatString, "strager", 2);
// Call the magic method...
ICollection<string> parts = String.ReverseFormat(formatString, s);
// parts now contains "strager" and "2".
I know I can use regular expressions to do this, but I would like to use the same format string so I only need to maintain one line of code instead of two.

Here is some code from someone attempting a Scanf equivalent in C#:
http://www.codeproject.com/KB/recipes/csscanf.aspx

You'll have to implement it yourself, as there's nothing built in to do it for you.
To that end, I suggest you get the actual source code for the .Net string.format implmentation (actually, the relevant code is in StringBuilder.AppendFormat()). It's freely available, and it uses a state machine to walk the string in a very performant manner. You can mimic that code to also walk your formatted string and extract that data.
Note that it won't always be possible to go backwards. Sometimes the formatted string can have characters the match the format specifiers, making it difficult to impossible for the program to know what the original looked like. As I think about it, you might have better luck walking the original string to turn it into a regular expression, and then use that to do the match.
I'd also recommend renaming your method to InvertFormat(), because ReverseFormat sounds like you'd expect this output:
.)s(woc 2 evah .regarts si eman yM

I don't believe there's anything in-box to support this, but in C#, you can pass an array of objects directly to any method taking params-marked array parameters, such as String.Format(). Other than that, I don't believe there's some way for C# & the .NET Framework to know that string X was built from magic format string Y and undo the merge.
Therefore, the only thing I can think of is that you could format your code thusly:
object[] parts = {"strager", 2};
string s = String.Format(formatString, parts);
// Later on use parts, converting each member .ToString()
foreach (object p in parts)
{
Console.WriteLine(p.ToString());
}
Not ideal, and probably not quite what you're looking for, but I think it's the only way.

Related

Does .Net contain a built in Line Endings conversion which checking input format [duplicate]

This question already has answers here:
Replace Line Breaks in a String C#
(20 answers)
Closed 5 years ago.
I'm getting a string from an external native library function which uses "\n" for line separation. I simply want to write this to disk using the appropiate line ending format on the system I'm running my .Net application, which will be usually windows. So "Environment.NewLine" results correctly in "\r\n".
I'm simply write the string to a textfile via
MyString = NativeLib.GetStringEitherLForCRLF();
File.WriteAllText(MyFile, MyString); // will not change LF format
This doesn't changes lineendings, which seems to be intented that way. However, I'm wondering while the same string in C using fwrite in asciimode would probably change the lineendings as I remember.
Since conversion would be an easy task I'm just curios, how the "correct" or best-practice way in .Net should be. Is there a standard function, which would automatically convert the lineendings according to the current environment settings ?
I know how to replace characters in strings, that is not the basic question. The exact question is:
Does there exist a .Net standard library function that will do line ending conversions already depending on OS and Environment settings or which maybe has to be used with a certain parameter to ensure this?
Like e.g.:
File.WriteAllText(MyFile, MyString, Lineencoding.CRLF); // which does not exist
or
File.WriteAllText(MyFile, MyString.ConvertLineendings(Line.CRLF)); // which also does not
But there maybe an option I've missed but it doesn't look like that.
I do not want to write unnecessary boilerplate code to such a trivial task.
The problem is also described in Normalize newlines in C# just to show that I can be more complex than most people think. I'm not completely sure whether the proposed regex solution will work in 100% of all cases.
If you really get the input as a string (as opposed to a file or something), then create a StringReader over it. On the reader, do .ReadLine() in a loop and write each line to your file (e.g. with a StreamWriter).

Trimming strings between two points

I have a string as follows:
ListViewSubItem: {Debian6/Debian6.vmx }
What would be the most efficient way of getting the following output:
Debian6/Debian6.vmx
int beginidx = haystack.IndexOf('{');
string needle = haystack.SubString(beginidx,
haystack.IndexOf('}') - beginidx + 1).Trim();
string result = Regex.Match("ListViewSubItem: {Debian6/Debian6.vmx }", #"(?<={)(.+?)(?=})").Value;
You can use a regex:
\{\s*(.*)\s*\}
The desired string will be in the first captured group (Match.Groups[1]).
Example:
string output = Regex.Match(input, #"\{\s*(.*?)\s*\}").Groups[1].Value;
As was pointed out, regexes are slower than plain string manipulation. If performance is an issue, and the string extraction is in a tight loop, then it may be better to use an optimized method. Otherwise, regex vs string is IMHO a matter of personal preference.
I'm going to guess you didn't generate that string yourself. So the question is, why parse it? This looks like the sort of question where you should take a step back and think of what you're trying to solve, and if another method might be cleaner.
I am not familiar with the ListViewSubItem class, but it seems like you have one of these and you have called ToString() on it. Now you are parsing the output of ToString() to see the model object your sub-item represents.
Does this not seem backwards to you? Maybe you should deal with the ListViewSubItem object itself (from a brief look at the documentation, what you want seems to be the Text property), rather than fiddling with what ToString() returns, which seems volatile and dependent on the implementation of that class's ToString() method (which, though likely not, could theoretically change from release to release). Not to mention corner cases like "what if the string contains the } character?"
For this in other reasons, as a general rule I think you should not have to deal with serialization when your data source is in memory the whole time.
So, in summary, if you have something like this:
ListViewSubItem item = /* ... */;
string s = item.ToString();
// TODO: parse {Debian} out of ListViewSubItem {Debian}
Why not this instead:
ListViewSubItem item = /* ... */;
string OS = item.Text;

C# - How can I convert an escaped string into a literal string? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can I expand a string that contains C# literal expressions at runtime
How can I convert an escaped string read from a file at runtime, e.g.
"Line1\nLine2"
into its literal value:
Line1
Line2
Amazingly I have found an example to do the opposite here using CSharpCodeProvider(), which seems like it would be the more difficult conversion. In order to do the opposite it appears I need to generate code to define a class and compile it in memory or execute a series of .Replace() calls, hoping I don't miss any escape sequences.
CSharpCodeProvider sounds like it certainly can do the trick here. However, I would ask 2 questions before using that: is it required to be exactly C# string literal syntax, and is the input file trusted?
CSharpCodeProvider obviously provides exactly the syntax of the C# compiler, but it seems to me like it'd be relatively easy for someone to inject some code into your process via this route.
The Javascript string literal syntax is fairly close to the C# string literal syntax, and .NET includes a JavaScriptSerializer class which can parse such string literals without injecting it as code into the running process.
Replace the escaped value by \n
static void Main(string[] args)
{
var test = "Line1\\nLine2";
// Line1\nLine2
Console.WriteLine(test);
// Line1
// Line2
Console.WriteLine(test.Replace("\\n", Environment.NewLine));
Console.ReadKey();
}

how to get a String with String.Format to execute?

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:
How to get String.Format not to parse {0}
Maybe, I just should put my code snippet in a method. But, what then?
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.
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
You could use RegEx to parse the three parameters out of the string, and then pass them to a real, actual string.Format method :-)
It looks like what you want is something like this:
string sbStr = string.Format("{0}----{1}", siDTO.SuggestionItemID, siDTO.Title);
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 }}.

Convert string to char

I get from another class string that must be converted to char. It usually contains only one char and that's not a problem. But control chars i receive like '\\n' or '\\t'.
Is there standard methods to convert this to endline or tab char or i need to parse it myself?
edit:
Sorry, parser eat one slash. I receive '\\t'
I assume that you mean that the class that sends you the data is sending you a string like "\n". In that case you have to parse this yourself using:
Char.Parse(returnedChar)
Otherwise you can just cast it to a string like this
(string)returnedChar
New line:
string escapedNewline = #"\\n";
string cleanupNewLine = escapedNewline.Replace(#"\\n", Environment.NewLine);
OR
string cleanupNewLine = escapedNewline.Replace(#"\\n", "\n");
Tab:
string escapedTab = #"\\t";
string cleanupTab= escapedTab.Replace(#"\\t", "\t");
Note the lack of the literal string (i.e. i did not use #"\t" because that will not represent a Tab)
Alternatively you could consider Regular Expressions if you need to replace a range of different string patterns.
You should probably write a utility function to encapsulate the common behaviour above for all the possible Escape Sequences
Then you'd write some Unit Tests to cover each of the cases you can think of.
As you encounter any bugs you add more unit tests to cover those cases.
UPDATE
You could represent a tab in the XML with a special character sequence:
see this article
This article applies to SQL Server but may well be relevant to C# also?
To be absolutely sure, you could try generating a string with a tab in it and putting it into some XML (programmatically) and using XmlSerializer to serialize that to a file to see what the output is, then you can be sure that this will faithfully 'round-trip' the string with the tab still in it.
how about using string.ToCharArray()
You can then add the appropriate logic to process whatever was in the string.
char.parse(string); is used to convert string to char and you can do vice versa
char.tostring();
100% solved

Categories

Resources