Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a word into character array

Tags:

string

c#

How do I convert a word into a character array?

Lets say i have the word "Pneumonoultramicroscopicsilicovolcanoconiosis" yes this is a word ! I would like to take this word and assign a numerical value to it.

 a = 1
 b = 2
 ... z = 26

 int alpha = 1;
 int Bravo = 2;

basic code

if (testvalue == "a")
{
    Debug.WriteLine("TRUE A was found in the string"); // true
    FinalNumber = Alpha + FinalNumber;
    Debug.WriteLine(FinalNumber);
}

if (testvalue == "b")
{
    Debug.WriteLine("TRUE B was found in the string"); // true
    FinalNumber = Bravo + FinalNumber;
    Debug.WriteLine(FinalNumber);
}

My question is how do i get the the word "Pneumonoultramicroscopicsilicovolcanoconiosis" into a char string so that I can loop the letters one by one ?

thanks in advance

like image 753
ETT Avatar asked Nov 30 '22 09:11

ETT


1 Answers

what about

char[] myArray = myString.ToCharArray();

But you don't actually need to do this if you want to iterate the string. You can simply do

for( int i = 0; i < myString.Length; i++ ){
  if( myString[i] ... ){
    //do what you want here
  }
}

This works since the string class implements it's own indexer.

like image 133
Øyvind Bråthen Avatar answered Dec 19 '22 06:12

Øyvind Bråthen