Converting text file to certain format c# [closed] - c#

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()));
}

Related

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());

c# split a string by nothing [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 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.

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]);
}

Is it possible in c# to do math with file names that contain numbers? [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
Is it possible in c# to do math with file names that contain numbers? (i.e. change
file name : "c:\123.img" to "c:\127.img"?
This should be enough to get you started:
var files = Directory.GetFiles(path);
foreach (var f in files)
{
var info = new FileInfo(f);
var name = info.Name.Split('.')[0];
var extension = info.Name.Split('.')[1];
int i;
if (int.TryParse(name, out i))
{
File.Move(info.FullName, string.Format(#"{0}\{1}.{2}", path, i + 5, extension));
}
}

Calculate molecular weight based on chemical formula [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 3 years ago.
Improve this question
I am trying to write a program which will calculate the molecular weight of a given molecule based on its chemical formula.
This code can split a molecular formula like "CH3OH" to an array {C H 3 O H} but from here, what would be a good way to use the split text to calculate the molecular weight?
string input = MoleculeTextbox.text;
string pattern = #"([0-9]?\d*|[A-Z][a-z]{0,2}?\d*)";
string[] sunstrings = Regex.Split(input,pattern);
First of all, you'd need to parse the string and turn "H3" into "HHH", etc. That might look something like this:
var x = "CH3OH".replace(/([a-z])([2-9])/gi, function(_,c,n) { return new Array(1+parseInt(n)).join(c); });
First group being the matched character, and second group being the number of repetitions.
You now have a string that looks like "CHHHOH". You can split that line into an array with one character at each index, .split(''), and map each value to its molecular mass. For that you need to define some sort of lookup table. I'm using reduce to take care of the mapping and the addition in one go:
var mass = { C: 12.011, H: 1.008, O: 15.999 };
var weight = x.split('').reduce(function(sum,element) { return sum + mass[element]; }, 0);

Categories