Strings in C++

Darc

Well-known member
Joined
Apr 18, 2003
Messages
89
I have the following function in VB .NET:
Code:
    Public Function GetDataFromString(ByRef str As String) As String
        Dim x As Integer = str.IndexOf(">")
        If x = -1 Then
            GetDataFromString = str
            Exit Function
        End If
        GetDataFromString = str.Substring(0, x)
        str = str.Substring(x + 1)
    End Function

what would a simple unmanaged C++ equivalent be? (Ive never used strings in C++ before)
 
something like this then ....
C#:
///C++

String* test(String* str)
{
	int x=str->IndexOf(">");
	if(x=-1) /// if its not found.
	{
        String* NotFound="> not found!";
		return NotFound;
	}
	else
	{
        return str->Substring(0, x);
	}

}
hope that helps you start off :)
 
the String class is .NET though, is it not? I want to use Unmanaged C++...
 
Heres a quick example. As you can see, its a bit convoluted.
Code:
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, void* argv[]) {
	char* str = "abcabc<defdef";
	char* match = strstr(str, "<");
	char substr[1024];
	
      // If no match, strstr points to a zero-length string
	if (strlen(match) == 0)
		return 0;
	
      // match is the address of the match, str is the address of the string,
      // so match - str is the length from the start of the string to the match.
	int substrlength = match - str;

      // Copy the first substrlength chars from the original into the substring.
      // Also, you need to null-terminate the string which is what the line below does.
	strncpy(substr, str, (size_t)substrlength);
	substr[substrlength] = 0;

	cout << substr << endl;

	return 0;
}
It will output abcabc to the console.

Note that strstr does not return the position of the substring inside the string, it returns the memory address of the match. For example, if "abcabc<defdef" is located at memory address 0x12345670, matching < with strstr will return a char* pointing to 0x1234676, because thats where the < is located.

If you dont understand the way memory is handled in C++, you best learn before you start trying to get your head around things like strings. Itll benefit you greatly.
 
Ive done lots of work with C++ before, but Ive never gotten into string functions.
bring on the challenge :)
 
Last edited by a moderator:
Ive managed to get it working except for one part. I want str to delete the returned value from itself (plus the "<") so:
Code:
Dim str as string = "abc^def"

Dim str2 as string = GetDataFromString(str)
str equals "def", str2 equals "abc"
 
Using the STL string class may make your life easier. Someone may argue that it sucks because its not a 0 terminated string. Thats their opinion. I say it rocks because its easier to use for string manipulations, and if I need a 0 terminated string, I can string.c_str() and be done with it. However, I will agree that using char pointers (const char*) for straight input and output (no manipulation) is better.

Code:
#include <string>

string GetDataFromString(string& str) 
{
   string::size_type pos = str.find(">");
   if (pos == string::npos) {
      return str;
   }
   str = str.substring(pos + 1);
   return str.substring(0, pos);
}

// To use;
string str = "abc^def";
string str2 = GetDataFromString(str);

// Or;
const char* str = "abc^def";
const char* str2 = GetDataFromString(str).c_str();
 
Im not familiar with doing strings between C++ and VB.NET, but Ill assume you cant. In which case, change the return type to const char* and change the return statement to str.substring(0, pos).c_str();
 
Back
Top