String Functions?

r-S

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

I am a new programmer to VC++ .NET - Im migrating from VB 6.0, which I used for around 3 years. Im 17 years old, from the UK, and Ive been interested in programming since the age of 11 - when I started playing around in QBasic :p . In the future, Im aiming for a job in software development, and it seems that for me C++ .NET is the way to that goal. Im currently midway through an ICT course at college - but hopefully Ill 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. Ive searched the forums, and found a little more information that I had to start with, but Im still lacking a bit.

There are a few functions Im 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()
Code:
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()
Code:
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 its 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.
Code:
Dim userInfo() as String
userInfo = sData.Split( ";") 


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

C++ isnt my strongest subject but I think you can declare a string without requiring a pointer.
 
Now, I dont think Im the right guy to reply because I dont 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 Im wrong, but if you call split like you did, it will take a string like "Hi;How are you;Im fine" and make it "Hi", "How are you", and "Im fine".

I dont 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 by a moderator:
Back
Top