devxlogo

C++ equivilant of Basic MID$

C++ equivilant of Basic MID$

Question:
I need to test for a certain char in a char array.For example, in BASIC I would have:

if mid$(string,1,1) = "`" then blah blah blah....

How would I do this in C/C++? Note: Iā€™m still new to C++ šŸ˜› Compiler == Turbo C++ 3.0(Dos)

Answer:
Normally, you could just use:

   if (string[0] == ā€˜`ā€™) ā€¦
This assumes that string is a character array (or pointer to a character array) and is much more efficient than BASICā€™s Mid$ function. (Note that the index for the first character is 0 and not 1).

If you are using a string class, the syntax might be a little different, but all good string classes provide a syntax for accessing individual characters and it often looks the same as that for character arrays.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist