Reputation: 1833
I'm trying to use C# to iterate through an array.
The array consists of the letters of the alphabet.
I'm doing this inline in the view, which I know is not ideal. But, for each letter I need to write HTML to the document.
Basically I need to do the following JS - just don't know the C# syntax:
var myArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //I think ToCharArray();
for (var i=0, i < myArray.length, i++)
{
if (somecondition)
{
write some html;
}
else
{
write some other html;
}
}
Right now there are 26 versions of this in a user control. I'm a front end developer and this was handed to me by the back end. Just trying to find a more efficient way to do it for maintainability's sake.
<% if (Convert.ToInt32(Model.State.Cities.GetCityCountByAlphabet("A")) == 0) { %>
<span class="no-link">A</span>
<% } else { %>
<a href="#to-A">A</a>
<% } %>
Upvotes: 0
Views: 8903
Reputation: 389
I actually did something like this a few years ago so I pulled up my code and modified it to try and show it as an example based on what you've shown:
<div id="azindex">
<ul id="index">
<%
var myArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
var letters = Model.State.Cities.Select(city => city.Name[0]).Distinct(); //assume array setup based on same database values
var letterArray = myArray.Where(chr => letters.Any(letter => letter == chr));
foreach (var letter in letters)
{
%>
<li><a href='#<%= letter %>'><%= letter %></a></li>
<%
}
%>
</ul>
</div>
Assuming using your example where you want to write out some other html when the condition doesn't match I'd modify my code somewhat similiar to the following uncompilable code:
for (var i=0; i < myArray.Length; i++)
{
var letter = myArray[i];
if (letters.Any(p => p == letter)
{
<span class="no-link">{letter}</span>
}
else
{
<a href="#to-{letter}">{letter}</a>
}
}
Upvotes: 1
Reputation: 660503
The other answers are reasonable, but personally I would be inclined to simply write
foreach(char c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
{ ... }
and skip the indexer, local variable, blah blah blah.
More generally: if your problem solving technique every time you don't know the syntax of C# is going to be to ask on StackOverflow, you're not going to be very efficient at work. Why not learn basic C#? If you already understand JavaScript it shouldn't be too hard to pick up the basic idioms and syntax of procedural code. The type system is rather different, but the basic control flows are quite similar.
Upvotes: 13
Reputation: 839134
The simplest way to iterate over the characters in a string is to use foreach
:
foreach (char c in yourString)
{
// ...
}
This will also work on an array.
If you just want to iterate over the letters A to Z in order you don't even need to store a string containing all the letters. You can instead use this:
for (char c = 'A'; c <= 'Z'; c++)
{
// ...
}
Upvotes: 5
Reputation: 4564
You can either use:
Char c = myArray[i];
or something like this:
StringBuilder html = new StringBuilder();
foreach(Char c in myArray)
{
if (c = something)
html.Append(somehtml);
else
html.Append(otherhtml);
}
I hope that this will help you.
Upvotes: 0
Reputation: 164341
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
foreach(char ch in characters)
{
if (somecondition)
{
Response.Write("some html");
}
else
{
Repsonse.Write("some other html");
}
}
Upvotes: 0
Reputation: 82654
It's just about the same if you want it to be:
var myArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //I think ToCharArray();
for (var i=0; i < myArray.Length; i++) {
char letter = myArray[0];
if (somecondition) {
write some html;
} else {
write some other html;
}
}
Upvotes: 0
Reputation: 13756
to access nth character of a string use
myArray[i]
that will do
Upvotes: 0
Reputation: 888195
C# strings can also be used like arrays:
for (int i = 0; i < str.Length; i++)
//str[i] is a char
Upvotes: 5