Newby String Question

  • Thread starter Thread starter kenoyer130
  • Start date Start date
Fred,

That is often (mostly) used in VBNet completly the same exept the semicolon
as well
i= mytxt.IndexOf("c");

And because it is a zero indexer it becomes -1 when there is no "c"

I hope this helps,

Cor
 
...
In VB.NET there is a simple function to tell if one string
is contained inside another. For example:

dim mytxt as string = "abcdef"
dim i as integer

i = InStr(mytxt,"c") // will return zero if the
// character "#" is not in the string mytext
or
// 3 - the position of the chracter in
// this case.


string mytxt = "abcdef";

int i = mytxt.IndexOf("c");

NOTE! As indexing starts at 0, the above results i 2 (not 3), and if the
string is not found IndexOf returns -1 (not 0).

// Bjorn A
 
You can acheive the same results with the following C# code:

string x = "abcdef";
int i = x.IndexOf("c", 0);
Console.WriteLine("Index of c = " + i);

HTH
Jason
 
Hi:

I'm a VB.NET programmer who has made amazing progress in the C# world in
about two weeks.

I've come accross something that I can't figure out - I've checked MSDN and
Google extensively so I'm obviously not looking in the right area.

In VB.NET there is a simple function to tell if one string is contained
inside another. For example:

dim mytxt as string = "abcdef"
dim i as integer

i = InStr(mytxt,"c") // will return zero if the character "#" is not
in the string mytext or
// 3 - the position of the chracter in
this case.

I can't find a C# equivalent! (And I know it's there somewhere!)

Any help would be GREATLY appreciated!

Thanks,

Fred
 
I believe that's called "string indexing." The string class has several
methods, and one can parse a string and deliver the occurence of a substring
or character.

It would be something like:

string x = "roll";
string y = "rock and roll still rules";
int z;

z=y.IndexOf(x);
 
Fred Nelson said:
In VB.NET there is a simple function to tell if one string is contained
inside another. For example:

dim mytxt as string = "abcdef"
dim i as integer

i = InStr(mytxt,"c") // will return zero if the character "#" is not
in the string mytext or
// 3 - the position of the chracter in
this case.

I can't find a C# equivalent! (And I know it's there somewhere!)

using Microsoft.VisualBasic;

[....]

int i = Strings.InStr(mytxt,"c");


<runs for the hills, cackling...>

Nah, use IndexOf, it's much nicer.
 
Back
Top