Sergey
Sergey

Reputation: 33

How to convert Word or Number to list of Letters or Numbers?

I'm new for C#, but have some basic question:

I need to convert some words, for example: "Hello"(string) to list of letters" "H","e","l","l","o". Or Number 3242, to list or array of numbers: [3,2,4,2].

How to do that? And how after that I can change any number or letter in lists? In Python its very easy. But in C# I don't know what to do.

Please provide LOGICAL and EASY solution. I don't need some 50 lines script which using 20 functions. I'm interested in basic understanding of that process with easy solution, if it existing.

Thank you guys, P.S: i'm using .Net 3.1

Upvotes: 1

Views: 945

Answers (3)

Abhinav Pandey
Abhinav Pandey

Reputation: 183

As Prasad already told you what you have to use, Let me give you some examples about how you going to do it:

For String Type

Lets say there is a string:

string text = ".Net is Awesome";

To store this string as char[]:

char[] CharArray = text.ToCharArray();

To store this string as List<char>:

List<char> CharArray = text.ToCharArray().ToList();

Interestingly, for string Type variable you can directly use .ToList():

List<char> CharArray = text.ToList();

For Int Type

Now lets say you have an int:

int num = 123456;

To store this int as char[]:

char[] numArray = num.ToString().ToCharArray(); //Stored as char array no arithematic operation can be done.

Using Convert.ToInt32()

To store this int as int[]:

int[] numArray = num.ToString().Select(x => Convert.ToInt32(x)).ToArray(); //Arithematic Operations allowed

To store this int as List<int>:

List<int> numArray = num.ToString().Select(x => Convert.ToInt32(x)).ToList(); //Arithematic Operations allowed

Using int.Parse()

To store this int as int[]:

int[] numArray = num.ToString().Select(x => int.Parse(x.ToString())).ToArray(); //Arithematic Operations allowed

To store this int as List<int>:

List<int> numArray = num.ToString().Select(x => int.Parse(x.ToString())).ToList(); //Arithematic Operations allowed

Upvotes: 1

Prasad Telkikar
Prasad Telkikar

Reputation: 16059

You can use ToCharArray() function to convert input string to character array. If it is a type other than string then first convert it to String

Copies the characters in this instance to a Unicode character array.

var input = "Hello world";
if(input is string)  //Convert word to an array of char
     return input.ToCharArray();
if(input is int) //Convert Number to an array of char
     return input.ToString()  //First convert number to string
          .Select(x => int.Parse(x.ToString())) //Iterate over each digit and convert to int.
          .ToArray(); //Convert IEnumerable to Array

Upvotes: 2

Amit Verma
Amit Verma

Reputation: 2490

Use below code

var inputStringData = "Hello";//string
var charArray = inputStringData.ToCharArray() ;


var inputNumberData = 3242;//Number
int[] result = inputNumberData.ToString().Select(o => Convert.ToInt32(o) - 48).ToArray();

Upvotes: 1

Related Questions