Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have the following function in VB .NET:

   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)

  • Leaders
Posted

something like this then ....

///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 :)

  • *Experts*
Posted

Here's a quick example. As you can see, it's a bit convoluted.

#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.

  • Administrators
Posted

some of these may help, but I warn you using C / C++ is nowhere near as easy as C# / VB for things like string handling.

 

http://publications.gbdirect.co.uk/c_book/chapter9/string_handling.html

http://www.cs.cf.ac.uk/Dave/C/node19.html

http://inti.sourceforge.net/tutorial/libinti/stringhandling.html

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

I've done lots of work with C++ before, but I've never gotten into string functions.

bring on the challenge :)

Edited by Darc
Posted

I've managed to get it working except for one part. I want str to delete the returned value from itself (plus the "<") so:

Dim str as string = "abc^def"

Dim str2 as string = GetDataFromString(str)
'str equals "def", str2 equals "abc"

Posted

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.

 

#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();

Gamer extraordinaire. Programmer wannabe.
Posted
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();
Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...