c# split a string by nothing [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to split a string which is "00608436483" into every single number
minArray = hoursMin[1].Split('');
min = Convert.ToInt32(minArray[0]);
hoursMin[1] is 00608436483
I want that i got an array ->
array[0] = 0
array[1] = 0
array[2] = 6
array[3] = 0
array[4] = 8
...
for example

You can get each character from the string and convert that to a number:
string s = "1234";
IEnumerable<int> values = s.Select(c => c - '0' /* which is ASCII value 48 */);
What this code does:
It selects every character.
For each character, it converts the characters ASCII value to the integer representation of it.
It yields an enumerable you can iterate over.

Related

Console.ReadLine().Contains Console.ReadLine()? why do i get false when the number is present in both? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Could someone help me why do i get true only if the numbers are the same? I need to find out if the second input is in the first one. True/false
int[] firstNumber;
firstNumber = new int[10];
firstNumber[0] =
Convert.ToInt32(Console.ReadLine());
int secondNumber =
Convert.ToInt32(Console.ReadLine()) ;
Console.WriteLine(firstNumber.Contains(secondNumber));
Edit. I need to get true for ex.:firstNumber 5214 and secondNumber 4
how can i get true for 5432(firstNum) and 3(secondNum)
Use strings instead of integers, which will use string.Contains instead of Array.Contains:
string firstNumber = Console.ReadLine();
string secondNumber = Console.ReadLine();
Console.WriteLine(firstNumber.Contains(secondNumber));

How can I add two strings into an int in a foreach loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code that helps me getting information from texts files , the problem is that i cant get to add all the strings founds in the texts files into one int.
in the first Entrada.txt i have a 23 and in the second one i have 45.
How can i add those 2 numbers together?
foreach(var impressora in ListaImp)
{
var Entrada = File.ReadAllText(impressora + #"\Entrada.txt");
MessageBox.Show("Entrada : " + Entrada);
}
Output: 2345.
I want it to be 23 + 45 = 68
You can do this:
int sub = 0;
foreach(var impressora in ListaImp)
{
var Entrada = File.ReadAllText(impressora + #"\Entrada.txt");
sub += int.Parse(Entrada);
}
MessageBox.Show(sub.ToString());

Converting text file to certain format c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a text file containing numbers and each number is separated by a tab. There are also several lines of these numbers. I wanted to read the file and store the file into an array but I am not sure how to do so.
Sample file content
137 12.36922 .28200 4312170 .0550 108.4431 14.9959
127 12.23045 .28200 10400044 140.9635 22.9278 19.8656
514 12.91381 .42300 12550428 .1157 61.7263 123.4808
209 12.26951 .28200 10432158 .0361 8.4094 69.3899
271 12.68842 .35250 91375 .0663 3.6094 25.2950
548 12.99388 .49350 2131433 .1386 .6384 78.6621
314 12.54900 .35250 12469075 .1451 44.1327 115.9872
1466 13.40586 .63450 27236 140.6160 53.3465 65.4476
55 11.97313 .21150 100246 .0911 63.5528 60.7556
27 11.66276 .21150 12353651 140.9790 42.3193 110.4559
44 11.81954 .21150 10420688 .0447 38.5853 3.6592
The easiest way is to use Linq:
var text = File.ReadAllText(#"Your file path goes here");
var result = text.Split(" \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)?.Select(num => double.Parse(num)).ToArray()
If you want sorted data then you can use OrderBy or OrderByDescending extension methods:
var result = text.Split(" \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)?.Select(num => double.Parse(num)).OrderBy(d => d).ToArray()
Assuming the numbers are floats (you can parse to any type you like, this is just an example) then you could do soemthing like this:
string myFileString = System.IO.File.ReadAllText(#"C:\Temp\MyFile.txt");
string[] myStringArray = myFileString.Split('\t');
List<float> myNumberList = new List<float>();
foreach (string s in myStringArray)
{
myNumberList.Add(float.Parse(s.Trim()));
}

Textbox value to array or list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I enter the value in textbox:
1,2,3,4,5,6,7...
How to into to int array or list and show value display ???
And stop when a user enters the wrong consecutive 3 times or total error of 5
//textbox is your textbox controls' name
string[] content = textbox.Text.Split(',');
//use linq convert stringp[] to int[]
int[] nums = (from num in content
select int.Parse(num)).ToArray();
I don't understand the last part of your question (the bit about stopping when a user enters something wrong... I guess you'd have to define "wrong" in this context). Regardless, the following code should take values entered into a text box (let's call it txtNumbers) and put it in an array of ints.
string[] splitContent = txtNumbers.Text.Split(new char[] { ',' });
int[] nums = new int[splitContent.Length];
for (int i = 0; i < splitContent.Length; i++)
{
nums[i] = Int32.Parse(splitContent[i]);
}

Autonumber with alternating number and letters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want an auto-numbering class in c# which would generate numbers of 8 digit length in the following format i.e. 1A2B3C4D..one number followed by one letter.Any suggestions??
Pseudocode for generating such string:
String result = "";
for ( int i = 0; i < 8 ; i++)
{
if ( i % 2 == 0)
{
// random(a,b) returns random value between or equal to a-b
result.append(random(0,9).toString());
}
else
{
result.append(random(65,90).toChar()); // Generating a random value between 65-90 (A-Z in ascii)
}
}
Edit:
Or as Sayse suggested:
String result = "";
for (int i = 0; i< 4; i++)
{
result.append(random(0,9).toString());
result.append(random(65-90).toChar());
}

Categories