Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Lenght() and Mid() function ???

Status
Not open for further replies.

daniloa

Programmer
Nov 23, 2001
27
BR
Hy, I'm a VB developer and now I'm using MS Visual C++ 6 to develop some codes to telephony. And I have a litle problem:

MSVC++ have some function like Len() and Mid() and InSTR() Vb functions ????

how can I know how many characters have a char variable ???

Very thanks,

Danilo Almeida ...
 
If you have a c-string (a char*, or char[]), then you can use strlen(char*) to get the length of the string. It is defined in the header <string.h> or the standard header <cstring> (use std::strlen(char*) ). For mid, just divide what strlen returns by 2 and that will give you the index to the middle point in the array of characters.

-Skatanic
 
If you have a char array let's say szChar:

[red]Len()[/red]
Code:
int len = strlen(szChar);

[red]Mid()[/red](to get a substring of length L starting from point P.)
If you know the length of the substring at compile time:
Code:
char szSubstring[11];           // SET UP BUFFER
char* p = szChar + P;           // SET POINTER TO POSITION
strncpy(szSubstring, p, 10);     // COPY
szSubstring[10] = 0;             // SET TERMINATOR

otherwise it will have to be allocated:
Code:
char* szSubstring = new char[length + 1];           
char* p = szChar + P;         
strncpy(szSubstring, p, length);     
szSubstring[length] = 0;
...
delete szSubstring; // AT SOME POINT...

[red]InStr()[/red](to search a string for another string)
Code:
strstr(szChar, &quot;searchfor&quot;);


of course you can use std::string which has built in methods for all this stuff.
 
if you user CString - Variables, which I recommand,
you can use MyVar.GetLength(), MyVar.Left(..), etc.
And it's much easier to build new strings using CString-Variables.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top