473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A simple ForEach Question

147 New Member
Hi

I have split text into an array using char[] and ToCharArray()

I want to do the below code using a loop instead of writing out each condition as an if statement. Would this be best as a loop or is it best to do each one individually? Also, if it is best to do as a loop, how??

if array[0] = "x" then do this.
if array[1] = "x" then do this
if array[2] = "x" then do this
if array[3] = "x" then do this
if array[4] = "x" then do this

My second question is how to join 3 arrays into a string. EG, if array 0,1,2 each represenst one letter it could spell the word egg (where 0=e 1 = e 2 = g). I would then need to say

if (group of arrays 0,1,2 = egg)
{
then do this
}

If any one can help, thank you

Dave
Oct 25 '07 #1
3 885
kunal pawar
297 Contributor
tried out
for(i=0;i<array.length;i++)
{
if(array(i)='x')
then do this
}
Oct 25 '07 #2
DaveRook
147 New Member
Thank you Kunal

It is really kind of you to help me!

Thank you again

Dave
Oct 25 '07 #3
Plater
7,872 Recognized Expert Expert
You're not in VB, arrays are denoted with the correct [ and ] and not ( and )
Switch to square brackets when referencing the index of an array.


Also, since you're working with characters and strings you could do this:
Expand|Select|Wrap|Line Numbers
  1. string puttogether="";
  2. puttogether+=mychararray[0];
  3. puttogether+=mychararray[2];
  4. puttogether+=mychararray[4];
  5.  
And the string would have your characters
Oct 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
8464
by: dr. zoidberg | last post by:
Hello, I'm creating simple menu. MySQL: +----+-----+----------------------+ | id | sid | Title | +----+-----+----------------------+ | 1 | 0 | Main Menu 1 | 2 | 0 | Main Menu 2 | ...
2
1583
by: Ivan Demkovitch | last post by:
Hi! I'm trying to parse RSS news feed with code as simple as possible. Simple XML: <?xml version="1.0"?> <rss version="2.0"> <channel> <item>
32
4094
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
8
1116
by: blackberryoctopus | last post by:
I am receiving 40 variables from an external source into my php script. I need to have for loop cycle through those and assign them to variables in the php script. I am unsure of the syntax, but it...
29
4197
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
7
1368
by: Tony Johansson | last post by:
Hello! Why does not this cause a compile error because a ulong has never been implicitly convertible to byte? ulong vektor = {100000,200000,300000}; foreach(byte b in vektor) {...
6
1660
by: Ronald Raygun | last post by:
I want to be able to randomly select the following from an array: 1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to...
5
1722
by: SM | last post by:
Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD>...
1
3342
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
1263
by: Tony Johansson | last post by:
Hello! I have the code below. In the previous code not shown below I have used linq to extract some row and these have been placed in the var variable result. In my code there is just one entry...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
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
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
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
5650
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,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.