String Functions?

r-S

Newcomer
Joined
Dec 10, 2004
Messages
7
Location
England
Hi everyone on .NET Talk!

I am a new programmer to VC++ .NET - I'm migrating from VB 6.0, which I used for around 3 years. I'm 17 years old, from the UK, and I've been interested in programming since the age of 11 - when I started playing around in QBasic :p . In the future, I'm aiming for a job in software development, and it seems that for me C++ .NET is the way to that goal. I'm currently midway through an ICT course at college - but hopefully I'll be able to enroll for a development course at Uni two years from now :D

Anyway, enough of my little introduction - I have a question which I was hoping someone could answer. I've searched the forums, and found a little more information that I had to start with, but I'm still lacking a bit.

There are a few functions I'm looking for, if anyone can provide a link or the information that would be great!

(all VB code I post is VB6.0, despite the "VB.net" at the top of my tags!)

len()
Visual Basic:
Dim userName as String
userName = "r-S"
msgbox(len(userName))
Would show a messagebox containing the number 3. How can I do this in C++ .NET? I have found in this forum the function strlen(), however this works only with char variables. The way I have declared the variable in question is
Code:
System::String *sText = Form1::Text1->Text;
Is this even the correct way to declare a string?

--------

split()
Visual Basic:
Dim userInfo() as String
userInfo = Split(sData, ";")
In VB6.0, this would split the string "sData" into parts around each ";". They are accesable by using userInfo(0) to get the first part, userInfo(1) to get the second part, and so on. Is there an equivelant function in C++ .NET?

Sorry for such a long question - I could find nowhere with this information (though I expect it's out there somewhere :o )

Thankyou for any help or links ;)
 
If you are using the .Net string data type then a lot of the 'old' functionality is available through the variable itself.

e.g.
Visual Basic:
Dim userInfo() as String
userInfo = sData.Split( ";") 

''''''''''''''''''''''''''
Dim userName as String
userName = "r-S"
MessageBox.Show(userName.Lengh)

C++ isn't my strongest subject but I think you can declare a string without requiring a pointer.
 
Now, I don't think I'm the right guy to reply because I don't know VB or C++.net, but I do know plain old C++ and I can show you the way to do that in that.

Using Character Arrays:
Code:
char userName[4];
strcpy(userName, "r-s");
MessageBox(NULL, userName, "User Name", MB_OK);

Using STL:
Code:
#include <string>
//...
std::string userName("r-s");
MessageBox(NULL, userName.c_str(), "User Name", MB_OK);

.Net is probably a lot more like the STL version, but you can use both in a .Net program.

EDIT: Forgot the split part, sorry
Correct me if I'm wrong, but if you call split like you did, it will take a string like "Hi;How are you;I'm fine" and make it "Hi", "How are you", and "I'm fine".

I don't think there is a function that will do this for you, but you could do something like this:
Code:
#include <vector>
#include <string>
using namespace std;

void Split(vector<string> &strlist, string &str, string delim)
{
	strlist.clear();
	int index=0;
	int oldindex=0;
	while((index=str.find(delim, oldindex))!=string::npos)
	{
		if(index==oldindex)
			index++;
		else
		{
			string newstr;
			newstr=str.substr(oldindex, index-oldindex);
			strlist.push_back(newstr);
		}
		oldindex=index;
	}
	if(oldindex<str.length())
	{
		string newstr;
		newstr=str.substr(oldindex, str.length()-oldindex);
		strlist.push_back(newstr);
	}
}

Then, when you need to use it you just do this:
Code:
vector<string> userInfo;
Split(userInfo, sData, ";");
 
Last edited:
Back
Top