C# string.format add a "-" value?
Asked Answered
P

1

6

I have a string.format issue ...

I'm trying to pass my invoice ID as an arguments to my program ... and the 6th argument always end up with "-" no matter what I do ( we must use the ¿ because of an old program ) ...

     public static void OpenIdInvoice(string wdlName, string IdInvoice, Form sender){
        MessageBox.Show(string.Format("¿{0}",IdInvoice));
        proc.Arguments = string.Format("{0}¿{1}¿{2}¿{3}¿{4}¿­{5}",
            session.SessionId.ToString(),
            Session.GetCurrentDatabaseName(),
            session.Librairie,
            wdlName,
            "",
            IdInvoice
        );
        System.Windows.Forms.MessageBox.Show(proc.Arguments);

In the end, "-" is always added to my formatted result, but only before my IdInvoice ... (so Id 10 ends up -10 in my Arguments )

now the fun part ... I hardcode some string and ...

if I pass -1 instead of an Id, I have --1 as a result and If I write "banana" ... i get "-banana" ...

I know I could just build the string otherwise ... but I'm getting curious as to why it happens.

Here's the screenshot  minus banana ? ...

EDIT :

thats the copy/paste of my code

var proc = new System.Diagnostics.ProcessStartInfo("Achat.exe");
        System.Windows.Forms.MessageBox.Show(string.Format("¿{0}",args));
        proc.Arguments = string.Format(@"{0}¿{1}¿{2}¿{3}¿{4}¿­{5}¿{6}",
            "12346", //session.SessionId.ToString(),
            "fake DB",//Session.GetCurrentDatabaseName().ToString(),
            "false", //session.Librairie.ToString(),
            "myScreenName", //wdl.ToString(),
            "123456",
            "Banana",
            "123456"
            //args.ToString(), 

        );
        System.Windows.Forms.MessageBox.Show(proc.Arguments);
        System.Windows.Forms.MessageBox.Show(args);

and thats the copy/paste of my text visualiser result :

12346¿fake DB¿false¿myScreenName¿123456¿­Banana¿123456

Pollerd answered 24/3, 2017 at 19:46 Comment(8)
have you tried re-typing the format string and the values manually?Subtrahend
Can you paste the code in and paste in the value from the text visualizer please? There can be hidden characters in the text files that your editor will not show, if you copy and paste the code into stack overflow it will copy the hidden characters too and we can inspect it for the extra bytes.Choline
You've got a soft hyphen in there. Try pasting it into Notepad++. Though from copying and pasting what you posted it's actually before the last argument.Nobody
not sure where that char would be hiding. I'll try to copy/paste it directly.Pollerd
Your existing string has an extra character already near the last question mark. "{0}¿{1}¿{2}¿{3}¿{4}¿­{5}".Length returns "24" but there are visibly only 23 characters. Use your arrow keys to move the cursor through the characters and you'll see you have to arrow over twice to move passed the very last question mark. I get the minus sign showing up already, will print out some bytes to see what's up.. edit: as you can see just pasting it into this comment added a minus sign..when I click "edit" I do not see it.Choline
@Choline Notice that it is showing up in your comment though.Nobody
@Nobody Yes see my edit, just copy and paste OP's code into the comment and it shows up.Choline
@Choline : good idea to check the lengh ... we were soo focused on the result and though there was something wrong with the .formatPollerd
N
5

You literally have an extra character before "{5}" that's called a soft hyphen. It's one of those weird characters that isn't always displayed. If you place your cursor after the "{" in "{5}" and press the left arrow and then press backspace it will actually delete it. That or you can try to use an editor like Notepad++ that will display it. I was able to find it by running the following code

var t = @"{0}¿{1}¿{2}¿{3}¿{4}¿­{5}";
foreach (var c in t)
{
    Console.WriteLine((int)c + " " + c);
}

which printed out

123 {
48 0
125 }
191 ¿
123 {
49 1
125 }
191 ¿
123 {
50 2
125 }
191 ¿
123 {
51 3
125 }
191 ¿
123 {
52 4
125 }
191 ¿
173 -
123 {
53 5
125 }
Nobody answered 24/3, 2017 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.