473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Newby String Question

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
Nov 17 '05 #1
8 1639
..indexOf
..lastIndexOf
etc.

Nov 17 '05 #2
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

Nov 17 '05 #3
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

Nov 17 '05 #4

"Fred Nelson" wrote...
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
Nov 17 '05 #5
use String.IndexOf

int i = myText.IndexOf("c")
"Fred Nelson" <fr**@smartybird.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #6
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" wrote:
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

Nov 17 '05 #7
In message <#Q**************@TK2MSFTNGP14.phx.gbl>, Fred Nelson
<fr**@smartybird.com> writes
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.

--
Steve Walker
Nov 17 '05 #8
Thank you all very much for your help!

It's working quite nicely now!

Fred

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
0
by: marco | last post by:
I'm trying to parse a xml bookmarkpage with php. I found a very useful example script about how you can parse a xml document with php. The scriptworks really smooth. The xml test document (See...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
2
by: johnnyG | last post by:
Greetings, I'm studying for the 70-330 Exam using the MS Press book by Tony Northrup and there are 2 side-by-side examples of using the SHA1CryptoServiceProvider to create a hash value from a...
5
by: alexrixhardson | last post by:
Hi guys, I am a newby in the C/C++ world, and I am beginning to work on a rather simple TCP/IP proxy application which must be able to handle large volume of data as quickly as possible. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.