C++Introduction to C++Language FeatureLearn C++

How to Combine Two Path Strings in C++ Builder

How to Combine Two Path Strings in C++ Builder

C++ Builder has a lot of specific methods in its SysUtils library that are included in the vcl and fmx libraries. Some of these are grouped s a Path Manipulation Routines that allows users to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtilsSystem.SysUtils libraries. These all methods are easy to use and easy to get or set file path strings in that operating system. These 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 exist or not in that given path. And we can combine two path strings as a new path string.

In this post, you’ll learn how to combine two path strings in C++ Builder, what the Combine Method is, what the Syntax of the Combine Method is, and examples of the Combine Method. By learning how to use Combine Method to combine two paths on Windows, it will help you to easily build C++ applications using the C++ IDE.

What is the Combine method in modern C++ ?

Combine Method (System::IOUtils::TPath::Combine) is a Path Manipulation Routine that combines two paths strings. We can call Combine to obtain a new combined path from two distinct paths. If the second path is absolute, Combine returns it directly; otherwise, Combine returns the first path concatenated with the second one.

What is the syntax of the Combine method in modern C++ ?

Here is the Syntax of Combine() Method:

The following table lists the parameters expected by this method:

NameMeaning
Path1The first path. Path1 is used as root for Path2.
Path2The path that is concatenated with Path1.

Note: Combine raises an exception if the given paths contain invalid characters.

As you see Path1, Path2 parameters and returning parameter are UnicodeString which means you can use any word language if your operating system support it.

Six simple examples of using the Combine method in C++

Combine Method returns a UnicodeString, we can check a file and set the result to a bool as in different examples below,

Example 1

Let’s combine two path Strings. The first one has a drive letter with a folder name and the second one has folder names,

and the output form of ShowMessage() will display result path String as below,

as you see we use “\\” to define single “\” in Strings. Combine Method() automatically adds “\” to between two paths if needed.

Example 2

Let’s try Example 1 with a “\” at the end of path1,

and the output form of ShowMessage() will display result path String as below,

as you see we use “\\” to define single “\” in Strings. Combine Method() automatically adds “\” to between two paths if needed.

Example 3

Let’s try Example 1 with a “\” at the end of path1,

and the output form of ShowMessage() will display result path String as below,

as you see Combine Method() automatically combines two paths without adding “\” to between two paths.

Example 4

Let’s try Example 1 with a “\” at the front of path2,

and the output form of ShowMessage() will display result path String as below,

as you see Combine Method() automatically combines two paths without adding “\” to between two paths.

Example 5

and this time the output form of ShowMessage() will display result path String as below,

as you see Combine Method() returns the second one, because both has different paths.

Example 6

and in this example the output form of ShowMessage() will display result path String as below,

as you see Combine Method() returns the second one, because the second includes the first one.

From these examples we can briefly say that if the second parameter has a drive with ‘:’ then it returns the combination of Path1+Path2 or Path1+ Path2 ;

Here is a full example of how to use the Combine method in C++

Here is the full C++ Builder VCL example with the Memo (TMemo) component below,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?