CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2016
    Posts
    8

    Working with verbatim strings

    I am trying to create a filename in a class that is part of a data collection program. The verbatim construct works properly in the main program. However in the class method, trying to create a string using:

    String teststring = @"\test stuff\;

    creates the string \\test stuff\\

    I also tried string teststring = "\\ test stuff\\; but got the same result.

    Included below is a simple test program I put together to see what is happening that duplicates the problem.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace String_Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                NewClass.stringTest();
                string test = @"/this is a test/";
                test = test + "more string";
                test = '/' + "more string";
                int a = 1;
    
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace String_Test
    {
        static class NewClass
        {
            static string filename;
    
            public static void stringTest()
            {
                filename = @"\this is a test\";
                int a = 1;
    
            }
        }
    }
    The string in Form1.cs is created properly but the string in the class has the extra / issue. Any ideas of why this would be happening. I am using WinForms and added the class using the add option on the solution drop down menu.

    Appreciate any help.

  2. #2
    Join Date
    Oct 2015
    Posts
    26

    Re: Working with verbatim strings

    For starters you have
    filename = @"\this is a test\";
    and
    string test = @"/this is a test/";


    2nd why are you using slashes unless you have a reason for them. They are not needed.

    3rd What string are you expecting to get?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured