Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mid(), Left(), InStr() and other functions like in VB? 1

Status
Not open for further replies.

hvytas

Programmer
Aug 10, 2005
23
LT
Hello for all C++ programmers,

I think we write the fastest and smallest code requare for manipulating with strings like done in VB (Visual Basic).
If somebody know how to write other functions that work faster (like with ASM or C++) and use less code, add to this thread.

copy mid point from string
Code:
string Mid(string& str, int pos1, int pos2)
{
	int i;
	string temp = "";
	for(i = pos1; i < pos2; i++)
	{
		temp += str[i];
	}

	return temp;
}

copy mid point vb style (pos + length)
Code:
string vMid(string& str, int pos1, int length)
{
	int i;
	string temp = "";
	for(i = pos1; i < pos1+length; i++)
	{
		temp += str[i];
	}
	return temp;
}

copy from left of pos
Code:
string Left(string& str, int pos)
{
	int i;
	string temp = "";
	for(i = 0; i < pos; i++)
	{
		temp += str[i];
	}

	return temp;
}

copy from right of pos
Code:
string Right(string& str, int pos)
{
	int i;
	string temp = "";
	for(i = pos; i < strlen(str.c_str()); i++)
	{
		temp += str[i];
	}
	return temp;
}

lowercase the string
Code:
string Lcase(string& str)
{
	int i;
	string temp = "";
	for(i = 0; i < str.length(); i++)
	{
		temp += tolower(str[i]);
	}
	return temp;
}

uppercase the string
Code:
string Ucase(string& str)
{
	int i;
	string temp = "";
	for(i = 0; i < str.length(); i++)
	{
		temp += toupper(str[i]);
	}
	return temp;
}

How to do InStr(), InStrRev() or other functions add if you know.
 
Obviously, these left/mid/right etc functions are cumbersome surrogates of std::substr (i.e. invention of the wheel).
Case translations with (by definition) ineffective +=? Better use local string copy (initialized by ctor) then change its buffer directly via const_cast<char*>(c_ptr()) pointer (then return it).
 
How about using std::string?

Ion Filipski
1c.bmp
 
Fine, this is std edition:]
If somebody know how to write shorter add to this thread.

Code:
int CGlobal::InStrRev(string str, string what)
{
	// int InStrRev(start-1, str, what)
	return str.find_last_of(what) + 1;
}

InStr, if return -1 that is wrong pos, default pos=0
Code:
int CGlobal::InStr(string str, string what, int pos)
{
	// int InStr(start-1, str, what)
	if (pos >= 0)
	{
		if (pos == 0)
		{
			return str.find_first_of(what) + 1; 
		}else{
			return str.find_first_of(what, pos) + 1;
		};
	}else{
		return -1;
	};
}

copy mid point from string (NEW*)
Code:
string CGlobal::Mid2(string str, int pos1, int pos2)
{
	string sFind = "";
	if (pos1 >= 0)
	{
		sFind.assign(str, pos1, pos2); // 0..n
	};
	return sFind;
}

This functions translated from VB for easy use string manipulation in C++ or use C++ in VB and etc.
 
or use C++ in VB?..
You can't use C++ std::string in VB b-string environment.

But what for these exercises at all? I think the C++ std;;string class has much more features than VB strings...
 
Code:
string CGlobal::Mid( const string& str, string::size_type pos1, string::size_type pos2 ) const
{
    return str.substr( pos1, pos2 );
}

string::size_type CGlobal::InStr( const string& str, const string& what, string::size_type pos ) const
{
    return str.find( what.c_str(), pos );
}
 
I guess all of you missunderstood me. std::string has all needed functionality to work with string as easy as in basic:
Code:
[COLOR=blue]#include[/color]<windows.h>
[COLOR=blue]#include[/color]<iostream>
[COLOR=blue]#include[/color]<string>

[COLOR=blue]using namespace[/color] std;

[COLOR=blue]int[/color] main()
{
  string x = "hello to my world";
  string y;
  cout<< "x = "<< x<< endl;

  y = x.substr(0, 5); [COLOR=green]//0 = left[/color]
  cout<< "y = left(x, 5): "<< y<< endl;

  y = x.substr(6, 2);
  cout<< "y = mid(x, 6, 2): "<< y<< endl;

  y = x.substr(9, 2);
  cout<< "y = mid(x, 9, 2): "<< y<< endl;

  y = x.substr(12, 5);
  cout<< "y = mid(x, 12, 5): "<< y<< endl;

  y = x.substr(9, 2);
  cout<< "y = mid(x, 9, 2): "<< y<< endl;

  [COLOR=green]//get from some position without number of 
  //chars needed
  //gets the string from the pos to end of string[/color]
  y = x.substr(9);
  cout<< "y = x from 9 to EOS: "<< y<< endl;

  [COLOR=green]//get from some position without number of 
  //chars needed
  //also means right, if you calculate it 
  //from the end if string
  // -1 needed because C++ uses 0 based indexes[/color]
  y = x.substr(x.length() - 1 - 5);
  cout<< "y = rigth(x, 5): "<< y<< endl;
[COLOR=green]
  //basic string is UNICODE, so use wide string
  //look at w prefix for string and cout[/color]
  wstring wx = L"hello world";
  wcout<< wx<< endl;

[COLOR=green]  //basic string:[/color]
  BSTR bstr = SysAllocString(wx.c_str());
  wcout<< bstr<< endl;
  SysFreeString(bstr);
  return 0;
}

this is the console output:
Code:
x = hello to my world
y = left(x, 5): hello
y = mid(x, 6, 2): to
y = mid(x, 9, 2): my
y = mid(x, 12, 5): world
y = mid(x, 9, 2): my
y = x from 9 to EOS: my world
y = rigth(x, 5):  world
hello world
hello world

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top