Question

In Visual FoxPro. there is a function addbs(), to which you pass a string.

If there is not a backslash at the end of the (trimmed) string, it will add one. If there is a backslash, it will return the trimmed string.

lcString = 'C:\Example'
lcNewString = addbs(lcString)
?lcNewString

Output:
C:\Example\

Is there an equivalent function in C#?

Was it helpful?

Solution

Maybe the Path class can help you out here? For example the combine() method.

OTHER TIPS

As the others already said, you probably want to use Path.Combine

However, you can easily create you own method to do that:

public string AddBS(string value)
{
    return value.EndsWith("\\") ? value : value + "\\";
}

To make it more generic, I'd suggest, making an extension like:

public static class StringExtensions
{
    public static string AddSuffix(this string value, string suffix)
    {
        return value.EndsWith(suffix) ? value : value + suffix;
    }
}

There is a free "Visual FoxPro Toolkit for .NET" which has C# and VB libraries which bring many of the FoxPro functions into .Net.

http://foxcentral.net/microsoft/vfptoolkitnet.htm

The Visual FoxPro Toolkit for .NET is a class library with over 225 Visual FoxPro functions for use with any .NET language. The included VFPToolkitNET.DLL is a .NET managed code DLL (not a COM wrapper) and does not require Visual FoxPro. The functions supported execute extremely fast since they are all written in .NET and compiled into a managed code .NET DLL. Included is a reference and tutorial stand-alone CHM help file as well as Visual Studio .NET integrated dynamic help using the same CHM help file. Full IntelliSense and Dynamic Help is enabled for the VFP functions when programming in any .NET language. Using the VFP Toolkit or .NET, most Visual FoxPro functions become available in Visual Basic .NET or any other .NET language. Functions like STRTOFILE() convert a string to a file in only one line of code.

further, it says this:

The Visual FoxPro Toolkit for .NET does not teach developers Visual Studio .NET programming, but it does enable developers to do .NET programming much quicker and write less code based on what they are familiar with combined with how these functions reduce coding overall. It is not pseudo-VFP syntax, it is "real" VFP syntax and works most naturally in Visual Basic .NET since it no namespacing is required. The Windows .NET Framework and Visual Basic .NET (or any .NET language) must still be learned. What this toolkit does is assist Visual FoxPro developers using .NET since there is a much quicker learning curve for .NET programming having these functions available. The Visual FoxPro Toolkit for .NET is not a replacement to any .NET language. It compliments and enhances the experience and productivity of .NET language programming.

I always used path.TrimEnd('\\') + "\\" in that case. And since I used that code pretty often, I wrote an extension method:

public static string AddTrailingSlash(this string s, char slash = '\\')
{
    return s.TrimEnd(slash) + slash;
}

You can easily do that by using the following code:

string lcString = @"C:\Example";

if(!lcString.Substring(lcString.Length -1, 1).Equals(@"\", StringComparison.InvariantCulture))
{
    lcString += @"\";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top