Strings in C++

Darc

Regular
Joined
Apr 18, 2003
Messages
89
I have the following function in VB .NET:
Visual Basic:
    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? (I've never used strings in C++ before)
 
something like this then ....
C#:
///C++

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

}
hope that helps you start off :)
 
Here's a quick example. As you can see, it's 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 that's where the < is located.

If you don't understand the way memory is handled in C++, you best learn before you start trying to get your head around things like strings. It'll benefit you greatly.
 
I've done lots of work with C++ before, but I've never gotten into string functions.
bring on the challenge :)
 
Last edited:
I've managed to get it working except for one part. I want str to delete the returned value from itself (plus the "<") so:
Visual Basic:
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 it's not a 0 terminated string. That's their opinion. I say it rocks because it's 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();
 
I'm not familiar with doing strings between C++ and VB.NET, but I'll assume you can't. In which case, change the return type to const char* and change the return statement to str.substring(0, pos).c_str();
 
Back
Top