|
How to Compare two Stringsand to copy string into other string using C# Code.
What is the syntax?
Is there any system defined function such as strcmp,strcpy as used in C
I am taken two values in strings ,
now I want to compare which one is greater.
I can do it treating values as int or to convert.ToInt32,
but requirement is to treat and compare them as strings.
|
|
|
|
|
simworld wrote: Is there any system defined function such as strcmp,strcpy as used in C
To compare 2 strings, you could use the String.Compare method.
To copy one string value into another, all you do is
<br />
string oldValue = "OldValue";<br />
string newValue = oldValue;<br />
|
|
|
|
|
|
There are several ways you can compare strings in C#. The simplest way is to just test using equality:
string a = "This is a test";
string b = "This is another test";
if (a == b) {
} However, this will do a case-sensistive, culture-sensitive compare which may not be what you want and isn't necessarily the most performant test.
The closest methods to strcmp in C# are going to be the String.Compare , String.CompareOrdinal or the String.CompareTo methods.
In order to copy a string, you can simply copy it. Strings in C# are immutable, so you will always get a new instance of the string:
string a = "This is a test";
string b = a; You can find all of the string methods here[^].
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
|
I have read about how to send parameters to an application at runtime but what if the program is all ready running? Here is where I am stuck...
1. Run my application to get it into taskbar.
2. Click on a special hyperlink to send a parameter to this application.
I have learned how to create a registry entry to launch my application when a link is launched. Here are the problems..
1. I don't want my app to run again if the link is clicked again.
2. I need to link to continue to get parameters as the links get clicked
Any help here would be great.
|
|
|
|
|
|
Hi..
I have a vc++ dll that returns a Bstr that i need to read out in c# the dll is called like (at the moment)
[DllImport("myDLL.dll")]
[return : MarshalAs(UnmanagedType.BStr)]
private static extern string Getbstr();
i have tryed
string tmp = Marshal.PtrToStringBSTR(Getbstr());
pvarData="02" + tmp;
etc.. but only seam to get garbage.. can anyone help me or point me to a web site that explains the formats bstr etc...or how to convert them.
many thanks
calvin
P.S please be kind i have only just learnt c++ and the dll returns a bstr as it is also shared with vb apps...
|
|
|
|
|
Calvin Streeting wrote: [DllImport("myDLL.dll")]
[return : MarshalAs(UnmanagedType.BStr)]
private static extern string Getbstr();
Where did you get your import statement from?
led mike
|
|
|
|
|
the dll is declared as in vc6++
MYDLL_API BSTR __stdcall getbstr();
So i googled and people were mentioning "MarshalAs" which seameed to complie fine, is there another way (hopefully without changing the dll)
many thanks
calvin
|
|
|
|
|
Calvin Streeting wrote: the dll is declared as in vc6++
MYDLL_API BSTR __stdcall getbstr();
So the C module is allocating the BSTR. How is the deallocation handled?
Calvin Streeting wrote: have tryed
string tmp = Marshal.PtrToStringBSTR(Getbstr());
Did you try
string tmp = Getbstr();
led mike
|
|
|
|
|
Hi... I think i got it now..
here is a more complete code...
vc++ dll
//----------------------------------------------
//header
SSFILEDLL_API BSTR __stdcall GetEmailSubject();
SSFILEDLL_API BSTR __stdcall GetEmailData(char *szString);
//----------------------------------------------
//code
SSFILEDLL_API BSTR __stdcall GetEmailSubject(){
return GetEmailData("__substg1.0_0037001E");
}
SSFILEDLL_API BSTR __stdcall GetEmailData(char *szString){
BSTR Message;
COleStreamFile osf;
if (myCSS.OpenStream(szString, osf)) {
DWORD dwLength = osf.GetLength();
LPSTR buffer=(LPSTR)_alloca(dwLength+1);
osf.Read(buffer,dwLength);
buffer[dwLength]='\0';
Message = SysAllocStringByteLen(buffer, lstrlen(buffer));
return Message;
}
else {
char *buff = "False";
Message = SysAllocStringByteLen(buff, lstrlen(buff));
return Message;
}
}
and then in c#
I used
//----------------------------------------------
//header
[DllImport("ssFileDLL.dll")]
private static extern IntPtr GetEmailSubject();
IntPtr ptr = GetEmailSubject();
string s = Marshal.PtrToStringAnsi(ptr);
if (s!="False")
pvarData="" + s;
else
pvarData="";
and that seems to work... the code is a two part affair.. the first vc++ dll deals with outlook msg files parsing them and getting the mapi properties...
the second part is a explorer shell extention that shows the mapi properties for folders that contain archived (saved out of outlook) folders containing msg files...
after a bit of frigging around i now have form,to,subject,attachment count, and i am working on the sent/recived date... that are returned as a FILETIME
so onward i march and many thanks for your help...
calvin
ps any comments on code are always apreciated as i am new to the language (vb is far mote forgiving than c,c#,c++ etc...)
|
|
|
|
|
Calvin Streeting wrote: here is a more complete code...
If you have the source code for the DLL why don't you create a C++/CLI mixed mode managed assembly that exposes a .NET class and encapsulates all the native to managed activities?
led mike
|
|
|
|
|
Ok.... back to google to learn what all that means ...
Many thanks
Calvin...
|
|
|
|
|
We have some great introductory articles here on CodeProject written by Nishant Sivakumar. In the site menu on the side of the page under "Chapters", click "Languages" and then click "C++ / CLI"
led mike
|
|
|
|
|
Is there any difference between the two? I couldn't find simple tutorial Thanks
|
|
|
|
|
Sections 1.6.7.4 and 10.8 of the C# spec cover events.
An event is basically a property whose type is a delegate.
|
|
|
|
|
hi
in my main app, i was set app icon, then create new solution (setup application), and add my app files to it, and build my setup app, but when i test setup app, my app icon lost in exe file, why and how to solve my problem ?
thanks.
|
|
|
|
|
I have had this issue in the past as well. What I did is I added the icon to the resources directory of the project, then set the icon to be included as CONTENT in the resulting exe (through the properties window). Then use this icon (the one in the resources directory) as the one for the project. Be sure that if you want to use this icon on shortcuts that you include it in the application directory, and set the Icon property of the shortcuts to be the icon in that directory. Let me know if I need to explain this any more.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
thanks for reply
do your intent is that i must add my icon to resource directory of my main app and in my solution, set build action to embedded as resource in properties window ?
thanks
|
|
|
|
|
I don't think it matters if it is in the resource directory or not, but yes, I do think that you either must copy it to the output directory or use it as embedded content. Otherwise, for some reason installing through setup does not maintain the image.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
thanks for reply
but when i use installShield, it is make setup without any problem!
|
|
|
|
|
I'm glad you got it working.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Hi all,
I've got a System.Version field which I'm accessing from a property thusly:
[XmlRootAttribute(ElementName="quote", IsNullable=false)]
public class QuoteData
{
[XmlAttributeAttribute( DataType = "dll_version" )]
public Version DLLVersion
{
get
{
return dllVersion;
}
set
{
dllVersion = value;
}
}
private Version dllVersion = new Version();
}
but when I try to serialize it I get the error: "There was an error reflecting property DLLVersion". Any suggestions as to what's causing the error?
TIA.
|
|
|
|
|
hey guys, got a problem over here which i cant seem to solve. i have 1 picturebox displaying an image, and the second thats drawn on top of it, does not allow the bottom one to show through. what can be done to resolve this. will i have to resort to drawing my complex shapes with gdi?
|
|
|
|