Asked by:
Replace two backslashes with one Not working

Question
-
User2037455357 posted
Hello there.
This is quite a simple issue or should be.
All i want to do within my code is replace "\\" with "\"
see example below.
string s = "test"\\"MoreTesting"; s = s.Replace(@"\\", @"\");
but the string does not change.
can someone help me please.
Tuesday, February 25, 2020 1:58 PM
All replies
-
User475983607 posted
string s = "test"\\"MoreTesting";
The two backslashes are required to escape the single backslash character because the backslash has special meaning within a string. Use the literal "@" if you want a single backslash in the code.
string s = @"test\MoreTesting";
A side from string basics. Your code is invalid and does not compile.
Tuesday, February 25, 2020 2:09 PM -
User2037455357 posted
Thank you for getting back to quickly.
What about taking a SSIS variable that has a filepath.
for example the SSIS variable would be
\\server.com\folder1\folder2\excelFile_2020.xlsx
string excelfilepath = @Dts.Variables["User::ExcelFilePath"].Value.ToString();
Tuesday, February 25, 2020 2:13 PM -
User475983607 posted
Thank you for getting back to quickly.
What about taking a SSIS variable that has a filepath.
for example the SSIS variable would be
\\server.com\folder1\folder2\excelFile_2020.xlsx
string excelfilepath = @Dts.Variables["User::ExcelFilePath"].Value.ToString();
I'm not sure what you are asking. Strings behave as written in the C# programming guide.
Are you receiving an error message? If so, what is the error message?
Tuesday, February 25, 2020 3:19 PM -
User2037455357 posted
i have a c# script task in my ssis package,
i am passing a ssis variable contains a excel filepath. = \\server.com\folder1\folder2\excelFile_2020.xlsx
when i debug the c# my filepath becomes = \\\\server.com\\folder1\\folder2\\excelFile_2020.xlsx
then i get an execption error ( Exception has been thrown by the target of an invocation )
i assume this is because of the extra backslashes,
Tuesday, February 25, 2020 3:47 PM -
User753101303 posted
Hi,
First the C# debugger is showing strings using the C# syntax. \ is used to introduce an escape sequence and needs to be escaped (or you can use the @ prefix). See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim or :
using System; namespace ConsoleDemo { class Program { public static void Main() { var v1 = "a\\b"; // debugger shows a\\b var v2 = @"a\b"; // debugger shows a\\b Console.WriteLine(v1==v2); // console shows True Console.WriteLine(v1); // console shows a\b } } }
Get back at the exception and if you can try to dump ex.ToString() to get the full exception chain and stack rather than maybe ex.Message as you are perhaps doing now...
Tuesday, February 25, 2020 4:17 PM -
User288213138 posted
Hi masterdineen,
masterdineen
i am passing a ssis variable contains a excel filepath. = \\server.com\folder1\folder2\excelFile_2020.xlsx
when i debug the c# my filepath becomes = \\\\server.com\\folder1\\folder2\\excelFile_2020.xlsx
I don't know why there are 2 more backslashes in front of your file path, but if you want to get the absolute path for the specified path string, you can try to use the Path.GetFull Path Method.
string path = Path.GetFullPath(@"\\\\server.com\\folder1\\folder2\\excelFile_2020.xlsx");
More information about Path.GetFullPath you can refer to this link: https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getfullpath?view=netframework-4.8
If you still have the question, please post your code.
Best regards,
Sam
Wednesday, February 26, 2020 5:22 AM -
User2037455357 posted
Hi Sam
C# requires an escape backlash for each “\” to display unless there is a @ at the
beginning. My file path is coming from a SSIS variable.
so i shall try your solution.
Regards
RobWednesday, February 26, 2020 6:31 AM