C++Builder How To Include Or Exclude Trailing Backslashes On Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Include Or Exclude Trailing Backslashes On Windows
By Yilmaz Yoru September 16, 2021

How can I add a trailing backslash to a path string? How can I remove trailing backslash from the end of path string? What is the C++ ExcludeTrailingBackslash method? What is the IncludeTrailingBackslash method? What is difference between the ExcludeTrailingBackslash and ExcludeTrailingPathDelimiter methods? What is the difference between IncludeTrailingBackslash and IncludeTrailingPathDelimiter methods?

C++ Builder has a lot of specific methods in its Для просмотра ссылки Войди или Зарегистрируйся library that is included in the VCL and FMX libraries. Some of these are grouped as a Для просмотра ссылки Войди или Зарегистрируйся that allow users to edit, extract, get and set drive name, directory name, file name and file extensions. These methods are combined in the Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйсяlibraries. These methods are easy to use and easy to get or set file path strings in that operating system. They can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files or directories to see if they are exist or not on a given path. We can also get some specific directory locations like the “Current Directory”.

What is the C++ ExcludeTrailingBackslash method ?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System.SysUtils.ExcludeTrailingBackslash) is a SysUtils Method that returns a path name without a trailing delimiter. This method is included for backward compatibility only. The Для просмотра ссылки Войди или Зарегистрируйся should be used instead. ExcludeTrailingBackslash returns a UnicodeString, with the trailing path delimiter (Для просмотра ссылки Войди или Зарегистрируйся, ‘\’ on Windows, ‘/’ on Linux, macOS and mobile platforms) removed. Note that ExcludeTrailingBackslash works with multibyte character sets (MBCS).

What is the syntax of the ExcludeTrailingBackslash method?​

Here is the ExcludeTrailingBackslash Method:
C++:
UnicodeString __fastcall ExcludeTrailingBackslash(const System::UnicodeString S); // overload

What is the C++ IncludeTrailingBackslash method ?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System.SysUtils.IncludeTrailingBackslash) is a SysUtils Method. It ensures that a path name ends with a delimiter. This method is included for backward compatibility only. Для просмотра ссылки Войди или Зарегистрируйся should be used instead. System.SysUtils.IncludeTrailingBackslash. IncludeTrailingBackslash ensures that a path name ends with the trailing path delimiter (Для просмотра ссылки Войди или Зарегистрируйся, ‘\’ on Windows, ‘/’ otherwise). Note that IncludeTrailingBackslash works with multibyte character sets.

What is the syntax of IncludeTrailingBackslash method ?​

Here is the ExcludeTrailingBackslash Method:
C++:
UnicodeString __fastcall IncludeTrailingBackslash(const System::UnicodeString S); // overload

What is a simple example of using the C++ ExcludeTrailingBackslash and IncludeTrailingBackslash methods?​

ExcludeTrailingBackslash and IncludeTrailingBackslash methods both return UnicodeString. We can use them as given below,
C++:
 String path1 = IncludeTrailingBackslash( L"D:\\MyFolder" );
 ShowMessage(path1);
 
 String path2 = ExcludeTrailingBackslash( L"D:\\MyFolder\\" );
 ShowMessage(path2);
and the outputs will be as follows respectively,
Код:
D:\MyFolder\
and,
Код:
D:\MyFolder

Here is a full example of how to use the C++ ExcludeTrailingBackslash and IncludeTrailingBackslash methods​

Here is the full C++ Builder VCL example,
C++:
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
 String path1, path2, path;
 
 path1 = L"D:\\MyFolder";
 path2 = L"D:\\MyFolder\\";
 
 Memo1->Lines->Add( "Path1: " + path1 );
 path = IncludeTrailingBackslash( path1 );
 Memo1->Lines->Add( "IncludeTrailingBackslash: " + path );
 
 Memo1->Lines->Add( "Path1: " + path2 ) ;
 path = ExcludeTrailingBackslash( path2 );
 Memo1->Lines->Add( "IncludeTrailingBackslash: " + path );
}