Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am having problems trying to pass a string parameter to a secondary service API call.The method does not recognize the string that i am passing.My code:

C#
StringBuilder builtString = new StringBuilder();
             string startQuote = "\""; //start quote
             string endQuote = "\""; //end quote
             string nextEntry = "\r\n"; //new line next entry
             string comma = ",";
             string slash = "\\";

             //we use batchcols to specifiy header info.
             string batchcols = "number,message";
             string number1 = "27730367747";
             string message = startQuote + "Testssss, thissss is a comma test.Please ignore plz." + slash + endQuote;
             Debug.WriteLine(message);

             builtString.Append(startQuote);
             builtString.Append(number1);
             builtString.Append(comma);
             builtString.Append(slash);
             builtString.Append(message);
             builtString.Append(endQuote);

             final = builtString.ToString();


while debugging my code the string im passing with escaped literals is:
Quote:
"\"27730367747,\\\"Testssss, thissss is a comma test.Please ignore plz.\\\"\""


But it outputs with debug.writeline(string) :(i need it to be this when passing into the method.).Can someone clarify where i am going wrong?
Quote:
"27730367747,\"Testssss, thissss is a comma test.Please ignore plz.\""


What I have tried:

I have tried string encoding with the literals.I Dont know why the string is not being recognized as the output string.I am not sure how and why it would need encoding.
Posted
Comments
Jochen Arndt 5-Feb-16 3:37am    
I have problems to understand what you want finally to achieve (how should the string look when passing it to the API).

A common method would be just building the string and replacing special characters afterwards according to the rules defined by the API.

If the API for example requires escaped quote characters like the C# compiler, do something like this afterwards:

final = builtString.Replace("\"", "\\\"");

or using hex codes:

final = builtString.Replace("\x22", "\x5c\x22");
JDevZA 5-Feb-16 4:14am    
Hi , your idea of replacing the special characters seem to have worked. Thank you! i have made a string manipulation worker method that does the replace.The API method accepts the batch string like so on:

string testBatch = "27730367747,\"test,test\"\r\n27832603166,\"test,test\"\r\n27781204137,\"test,test\"";

so i just had to manipulate it to its preferred format with the string literals which where giving me the problem.
Jochen Arndt 5-Feb-16 4:24am    
Thank you for your feedback and fine to see that you have solved the problem.
Richard MacCutchan 5-Feb-16 3:55am    
What you see in the debugger is a copy of what you write in your code. What you display with writeline is what is actually transmitted inside your program.
JDevZA 5-Feb-16 4:16am    
This is not the case.when i debugged the logic.i was getting this on my debugger:

"\"27730367747,\\\"Testssss, thissss is a comma test.Please ignore plz.\\\"\""

and when i writeline the logic i got:
"27730367747,\"Testssss, thissss is a comma test.Please ignore plz.\""

Which had me dumbfounded...

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900