How to set space between TextBox values into array - c#

How to set space between TextBox values into array?
TextBox values:
1 32 54 76 89 89 454 23
Expected output array:
value[1]=1;
value[2]=32;
value[3]=76
...

Try this:
var numbers = textBox1.Text.Split(' ');
List<int> lst = numbers.Select(item => int.Parse(item)).ToList();
It will be even better if you use method group like this:
List<int> lst = numbers.Select(int.Parse).ToList();
Then you can get it's values like this:
lst[0] --> 1
lst[1] --> 32
and ...

You first have to split the users input:
var values = myTextBox.Text.Split(' ');
Now you can easily set your array of integers:
var myArr = values.Select(x => Convert.ToInt32(x)).ToArray();
Alternativly you can also use Int.Parse instead of Convert.ToInt32.

String[] values = tv.Text.Split(' '); // the values as string
int[] arr = new int[values.Length]; // the values as integer
for (int i = 0; i < arr.Length; i++)
arr[i] = int.Parse(values[i]);

If I understand you correct you need to create int array from string you can do it like this:
string TextBoxText = "1 32 54 76 89 89 454 23";
var value = TextBoxText.Split(' ').Select(Int32.Parse).ToList();
Then you can use your array like this:
Console.WriteLine(value[1]);
Console.WriteLine(value[2]);
TextBoxText should be Text property of your TextBox component.

Related

How to sort an array of string representing numbers (positive and negative as well as having a decimal part)?

With Windows form application, I select a txt file with random numerical values and I can print it on the screen properly. But "Array.Sort (values)" didn't work when I wanted to sort the values. How can I handle this?
Button Click Function
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = #"C:\",
Title = "Title",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "txt",
Filter = "txt (*.txt)|*.txt",
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
string path = openFileDialog1.FileName;
string[] txtDoc = File.ReadAllLines(path);
textBox6.Text = path;
Array.Sort(txtDoc);
foreach (string s in txtDoc)
{
txtDoc = s.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string ss in txtDoc)
{
richTextBox1.Text +=ss+"\n";
}
}
}
}
OUTPUT
10
2
5
-4
12,37
2
69
45
-4,41
35
76
35
-45
6
10
5
4
12
78
25
1
Sample txt
10 2 5 -4
6 10 5 4 12
35 -45
12,37
2 69 45 -4,41
35 76
78 25 1
You could sort the numbers with LINQ and parse them with double.TryParse(it seems you use comma as decimal separator):
string[] sortedNumbers = txtDoc
.SelectMany(line => line
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Select(token => double.TryParse(token, out double value) ? value : (double?)null)
.Where(nullableDouble => nullableDouble.HasValue)
.Select(nullableDouble => nullableDouble.Value))
.OrderBy(value => value)
.Select(value => value.ToString())
.ToArray();
Array.Sort() is not working because you are trying to sort by string i.e trying to sort lines present in the text file instead of sorting all integers present in that file.
To solve your problem,
You need to read all lines from your file, This step is already done in your code.
Split each line by , then convert all strings into an array of double.
Then Sort it in ascending order.
Now update your richTextBox1
string[] txtDoc = File.ReadAllLines(path);
var sortedList = new List<double>();
foreach(var line in txtDoc)
{
var value = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
.Select(double.Parse)
.ToList()
sortedList.AddRange(value);
}
sortedList.Sort();
richTextBox1.Text = string.Join(Environment.NewLine, sortedList);
if txtDoc is an array that contains all your strings, you may order it this way:
string[] orderedList =
txtDoc.OrderBy(x => new string(x.Where(char.IsNumber).ToArray())).ToArray();

How to access the string array elements for search?

I have a text file that include of numbers and I save it in a string array.
one line of my text file is this:
2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21
every line save in one of indexes of string array.
now how can i access array elements as int type and search and calculate in all of array?
this is my array:
string [] lines = File.ReadAllLines(txtPath.Text);
for example I want to return indexes of array that include number'14' in all of array .
This is the easiest and clearest way to solve it. I commented so you can better understand what happens in the entire program.
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
int integerNumber;
// checking if the string lineElements[j] is a number (because there also this case 114:275:5)
if (int.TryParse(lineElements[j], out integerNumber))
{
// if it is we check if the number is 14, in that case we add that index to the indexes list
if (integerNumber == 14)
{
indexes.Add(j);
}
}
}
}
// Printing out lines and indexes:
foreach (int key in dict.Keys)
{
Console.WriteLine(string.Format("LINE KEY: {0}", key));
foreach (int index in dict[key])
{
Console.WriteLine(string.Format("INDEX ELEMENT: {0}", index));
}
Console.WriteLine("------------------");
}
Console.ReadLine();
}
}
UPDATE 1:
As you requested:
special thanks for your clear answering.if i want to do search for all of my array elements what can i do? it means instead of only
number'14' i want to print indexes of all numbers that appear in
indexes
If you want to print all the indexes you should Console.WriteLine(j), that is the index of the inner for cycle, instead of checking the number value if (integerNumber == 14).
So, this is the program:
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
// printing all indexes of the current line
Console.WriteLine(string.Format("Element index: {0}", j));
}
}
Console.ReadLine();
}
}
UPDATE 2:
As you requested:
if i want to search my line till first " : " apper and then search next line, what can i do?
You need to break the for cycle when you are on the element with :
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
// I'm saving the content of lineElements[j] as a string
string element = lineElements[j];
// I'm checking if the element saved as string contains the string ":"
if (element.Contains(":"))
{
// If it does, I'm breaking the cycle, and I'll continue with the next line
break;
}
int integerNumber;
// checking if the string lineElements[j] is a number (because there also this case 114:275:5)
if (int.TryParse(lineElements[j], out integerNumber))
{
// if it is we check if the number is 14, in that case we add that index to the indexes list
if (integerNumber == 14)
{
indexes.Add(j);
}
}
}
}
// Printing out lines and indexes:
foreach (int key in dict.Keys)
{
Console.WriteLine(string.Format("LINE KEY: {0}", key));
foreach (int index in dict[key])
{
Console.WriteLine(string.Format("INDEX ELEMENT: {0}", index));
}
Console.WriteLine("------------------");
}
Console.ReadLine();
}
}
As you can see, if you run this piece of code and compare it with the first version, in output you'll get only the index of the first 14 occurrence, because the second one is after the string with :.
First you must get all conttent of file in the string array format:
public string[] readAllInFile(string filepath){
var lines = File.ReadAllLines(path);
var fileContent = string.Join(' ',lines);//join all lines of file content in one variable
return fileContent.Split(' ');//each word(in your case each number) in one index of array
}
and in usage time you can do like this:
var MyFileContent = readAllInFile(txtPath.Text);
int x= Convert.ToInt32(MyFileContent[2]);
IEnumerable<int> numbers = MyFileContent.Select(m=> int.Parse(m);)
var sumeOf = numbers.sum();
you can use linq to have more tools on collections.
var linesAsInts = lines.Select(x => x.Split(' ').Select(int.Parse));
var filteredLines = linesAsInts.Where(x => x.Contains(14));
// define value delimiters.
var splitChars = new char[] { ' ', ':' };
// read lines and parse into enumerable of enumerable of ints.
var lines = File.ReadAllLines(txtPath.Text)
.Select(x => x.Split(splitChars)
.Select(int.Parse));
// search in array.
var occurences = lines
.Select((line,lineIndex) => line
.Select((integer, integerIndex) => new { integer, integerIndex })
.Where(x => x.integer == 10)
.Select(x => x.integerIndex));
// calculate all of array.
var total = lines.Sum(line => line.Sum());

Split Array into 2D based on 2 parameters C#

I have a text file which I have split up into a string array based on new line.
string[] arr = s.Split('\n');
Now, I need to further categorize this into a 2-dimensional array wherein each column is a new "transaction".
So the text file basically contains info about bank transactions, an example being given below:
21......
22....
23.....
31....
32.....
31.....
32.....
21....
21.....
22....
The beginning of the numbers signify a new tx record which begins at a new line. I want to make it into a 2D array wherein each column is grouped as one tx beginning from 21 until it comes across the next 21 (so the record before it).
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].StartsWith("21"))
{
indices[i] = i;
}
}
I tried to write the code above to check for array element beginning with 21 and then storing the index but it ends up storing all the indices.
Any help will be appreciated!
What you'd need to do is
string[] arr = s.Split('\n');
List<List<string>> listOfLists = new List<List<string>>(); //dynamic multi-dimensional list
//list to hold the lines after the line with "21" and that line
List<string> newList = new List<string>();
listOfLists.Add(newList);
for(int i = 0; i < arr.Length; i++)
{
if(arr[i].StartsWith("21"))
{
if(newList.Count > 0)
{
newList = new List<string>(); //make a new list for a column
listOfLists.Add(newList); //add the list of lines (one column) to the main list
}
}
newList.Add(arr[i]); //add the line to a column
}
If I understand you right, you can try regular expressions (i.e. instead of splitting, extract transactions):
using System.Linq;
using System.Text.RegularExpressions;
...
string line = "21 A 22 B 23 C 31 D 32 E 31 F 32 G 21 H 21 I 22 J";
var result = Regex
.Matches(line, "21 .*?((?=21 )|$)")
.OfType<Match>()
.Select(match => match.Value)
.ToArray(); // <- let's materialize as na array
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
21 A 22 B 23 C 31 D 32 E 31 F 32 G
21 H
21 I 22 J

How to remove a duplicate number from an array?

Hi I'm working on this simple program that takes 5 numbers from user as long as the numbers are greater than 10 and less than 100. My goal is to remove duplicates numbers an ONLY show the NOT DUPLICATE numbers. Let's say if I enter 23 , 23, 40, 56 , 37 I should only output 40 , 56 , 37. Please help me on this. Thanks in advance. Here's my code:
static void Main(string[] args)
{
int[] arr = new int[5];
for (int i = 0; i < 5; i++)
{
Console.Write("\nPlease enter a number between 10 and 100: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number > 10 && number <= 100)
{
arr[i] = number;
}
else {
i--;
}
}
int[] arr2 = arr.Distinct().ToArray();
Console.WriteLine("\n");
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine("you entered {0}", arr2[i]);
}
Console.ReadLine();
}
One way is to group the elements based on input number and filter groups whose count is 1
int[] arr2 = arr.GroupBy(e=>e)
.Where(e=>e.Count() ==1)
.Select(e=>e.Key).ToArray();
Demo
I think you are looking for this:
int[] arr2 = arr.GroupBy(x => x)
.Where(dup=>dup.Count()==1)
.Select(res=>res.Key)
.ToArray();
Input Array : 23 , 23, 40, 56 , 37
Output Array : 40 , 56 , 37
How it Works:
arr.GroupBy(x => x) => give a collection of {System.Linq.GroupedEnumerable<int,int,int>} where x.Key gives you the unique elements.
.Where(dup=>dup.Count()==1)=> Extracts the the KeyValuePairs that contains Values count exactly equal to 1
.Select(res=>res.Key) => will collects the Keys from the above result
In your case, perhaps a combination of LINQ methods would be needed:
int[] arr2;
int[] nodupe = arr2.GroupBy(x => x).Where(y => y.Count() < 2).Select(z => z.Key).ToArray();

c# decimal to hex & hex to decimal / hex to ascii in LINQ

I'm trying to do some conversion and would like to use Linq to achieve the following,
Decimal to Hex,
Decimal to Ascii,
Hex to Decimal,
Hex to Ascii
Can someone please show me how to do this efficently in Linq?
I'll be displaying the output into textboxes.
Also, I have a prefix and delimiter field that will also need to be included,
Example:
string input = txtAscii.Text;
string delim = txtDelimiter.Text;
string prefix = txtPrefix.Text;
if (checkBox1.Checked == true && string.IsNullOrEmpty(delim)) delim = " ";
//Linq, Ascii to Decimal.
txtDecimal.Text = string.Join(delim, input.Select(c => prefix + ((int)c).ToString()));
Thanks all.
LINQ is for querying collections, not converting values. It is wrong to say you want to use LINQ to convert X to Y.
That said, here's the building blocks you need:
// string in decimal form to int
Int32.Parse("12345");
// string in hexadecimal form to int
Int32.Parse("ABCDE", NumberStyles.HexNumber);
// int to string in decimal form
12345.ToString();
// int to string in hexadecimal form
12345.ToString("x");
Then to do something like converting between decimal form to hexadecimal form:
var inDecimal = "12345";
var asInt = Int32.Parse(inDecimal);
var asHex = asInt.ToString("x");
Your "ASCII to (hexa)decimal" conversions could be done with a little bit of LINQ using the above building blocks. Assuming you mean the (hexa)decimal representation of each character's ASCII code:
var str = "FOOBAR!";
var asAsciiInt = String.Join(" ", str.Select(c => (int)c));
var asAsciiHex = String.Join(" ", str.Select(c => ((int)c).ToString("x2")));
// asAsciiInt == "70 79 79 66 65 82 33"
// asAsciiHex == "46 4f 4f 42 41 52 21"
var asciiInt = "70 79 79 66 65 82 33";
var charStrs = asciiInt.Split();
var asStr = String.Concat(charStrs.Select(cs => (char)Int32.Parse(cs)));
// asStr == "FOOBAR!"
var asciiHex = "46 4f 4f 42 41 52 21";
var charStrs = asciiHex.Split();
var asStr = String.Concat(charStrs.Select(cs => (char)Int32.Parse(cs, NumberStyles.HexNumber)));
// asStr == "FOOBAR!"
To convert numeric value to hex is dead easy.
Decimal d = 10M;
d.ToString("X")
I am however utterly confused by what you mean "decimal to ascii"
I don't really understand why you'd need to use LINQ here, but you can do the conversion between decimal and hex like this:
decimal dec1 = 182;
//convert decimal as a hex in a string variable
string hex = dec.ToString("X");
//convert the hex string back to the decimal
decimal dec2 = decimal.Parse(hex, System.Globalization.NumberStyles.HexNumber);

Categories

Resources