Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

concat

Hello Guys,
Can someone help me please!!!
How can I concatinate strings in c++

1.
like in Visual Basic just simply use & 
            String1 = "This is String1"

            String = String1 & " This is String2"

            String will become "This is string1 This is String2"

how about in C++ what is the best Operator

2. and in MFC what is the use of "->" operator and how can I use it


0
perci
Asked:
perci
  • 3
  • 2
  • 2
  • +1
1 Solution
 
perciAuthor Commented:
please answer
0
 
bkrahmerCommented:
1.  CString (MFC) and std::string (STL) both have + and += operators.  Just make sure your lhs is an object.  The rhs can be an object or a quoted literal.  

2. -> is the deference operator.  It dereferences a pointer.  You can use it with classes or structures.

typedef struct
{
  char a;
  int b;
} foo;
...
foo *fooPtr;
...
char x = fooPtr->a;

brian
0
 
violent1Commented:
the -> is used when dealing with pointers and has nothing more to do with MFC than in any other C++ application, in particular, when you have a pointer to a variable, and you want to call a function/method that is a part of that variable.  that variable, then, must have functions associated with it, and so will more than likely be a class.  you don't need it for strings.

for strings you have a couple of ways to go, but it depends on what kind of strings you are using.

for intance, you can create your own null-terminated strings with an array of char and then can create a concat operation yourself.  likewise, by including cstring.h you can use strcat(string1, string2);
but you must note that string1 and string2 or simply null-terminated, array based strings.

when dealing with the String class used within MFC, since you mentioned it, you can just use the + operator to concat strings together.  nifty!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
violent1Commented:
to clarify the -> it doesn't have to be to a method or function, it's just that is the usual case because most member variables of classes are protected or private and won't be accessible unless you are building an inherited class.

0
 
violent1Commented:
and also note that -> is not the only dereference operator.  * can be used to dereference as well.  it all depends on the situation.

0
 
gotenksCommented:
#include <string.h>

int main() {
   char mystring[50];

   strcpy(mystring, "hello");
   printf("%s\n", mystring); // prints "hello" to console

   strcat(mystring, " world");
   printf("%s\n", mystring); // prints "hello world" to
                             // console.

   return 0;
}

to concatenate a string, use strcat().
about MFC's '->'... this does not only appear in MFC but in c/c++ too... this is use to refer to a member of the data structure.

let say you have a structure with the following members :

typedef struct _MY_STRUCT {
   int value;
   int count;
} MY_STRUCT;

when you have a structure like the one above, you should be wondering how am i suppose to refer to variable "value", or variable "count" in coding. therefore, depending on how you declare your variable of that structure type to be, whether as a pointer to or a variable, you use the '->' or '.' to refer to them, respectively.

int main() {
   MY_STRUCT first, * second;

   first.value = 10;
   first.count = 0;

   second->value = 20;
   second->count = 0;

   return 0;
}

you should see that the above code illustrate to different approach to access the member of the data structure. one using the '.' and the other using '->'.
the reason is clear if you look at the variables declared in the first line of code of the main().
first is defined as a variable of type MY_STRUCT, while
second is defined as a POINTER to the variable of type MY_STRUCT.
as "second" is a pointer, therefore we use '->' to refer to the member value.

as the above is true for c/c++ data structure, it is also true for c++ class. but it might be lengthy to explain the latter. therefore if you were to swift from VB to C/C++, it is advisable to get yourself a tutorial on C/C++.
good luck.

gotenks.
0
 
perciAuthor Commented:
can you give me some good example please of no. 1, of concatinating 2 variables.
0
 
bkrahmerCommented:
std::string s1, s2, s3;

s1 = "a";
s2 = "b";
s3 = s1 + s2;
s3 = s1;
s3 += s2;
s3 += "c";

thanks,
brian
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now