How to split part of string - c#

I have this string
1 3 9 13 38 40 52 54 59 98 107 113:274:8 8 9 5 20 5 25 14 5 10 10
I need to split numbers so use this code
string[] lineElements = lines[i].Split(' ');
but I don't want numbers after first " : "
I only want to split this
1 3 9 13 38 40 52 54 59 98 107 113
what can I do?

You already have the answer, use String.Split
string[] lineElements = lines[i].Split(':')[0].Split(' ');
You may have issues if the string does not contain :, but I do not think so since you only want the first string.

Related

how to create a matrix with 1 for loop c#

How can i make a matrix as the one seen below, but with simply 1 for loop (instead of two):
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
Ive made the one above using 2 nested for loops, the first to create the rows and the second for the columns. How can i achieve the same result but with one for loop? Preferably implementing the / and % operators.
It is enough to have a loop from 1 to 63. Divide by 8 for the row and calculate the remainder by 8 for the column (plus one is because the loop starts from zero):
int[,] matrix= new int[8,8];
for (int i = 0; i <= 63; i++)
matrix[i/8,i%8] = i+1;

How to split a string into a array of arrays of strings as fast as possible in C#?

Short description: Splitting a string takes way to long.
Longer description:
I need to extract information from a string looking like this:
...
5 1 12 1 1 1 466 1277 458 80 92 Assistance
2 1 13 0 0 0 1055 1277 1717 100 -1
3 1 13 1 0 0 1055 1186 1717 191 -1
4 1 13 1 1 0 1055 1277 1717 100 -1
5 1 13 1 1 1 1055 1279 288 78 90 Vehicle
5 1 13 1 1 2 1489 1279 228 98 67 Lights
5 1 13 1 1 3 1856 1281 286 95 74 System
5 1 13 1 1 4 2284 1281 196 95 70 Apps
5 1 13 1 1 5 2618 1277 154 80 77 Info
...
(Side Note: the string comes as a return from the page.GetTsvText(0) method; page is a return of TesseractEngine.Process(image); so the string contains information about detected OCR strings, conficendes, bounding boxes coords, etc.)
In order to be able to make easier use of the information, I wrote a method that turns the string into a array of arrays of strings:
private string[][] getDataArray(string source)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine(source);
string[] rows = source.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
int nrOfRows = rows.Length;
string[][] result = new string[nrOfRows][];
for (int i = 0; i < nrOfRows; i++)
{
result[i] = rows[i].Split(new char[] { ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries);
}
sw.Stop();
Console.WriteLine(" $$$ getDataArray() took: " + sw.ElapsedMilliseconds + " ms");
return result;
}
Note: For some reason the string contains spaces that look longer than the usual spaces. I took it with copy-paste from the console log. It is a single character, not a tab, but it takes more space/ is wider than the usual space char.
Problem:
When I measure the time from inside the method, it takes less than 1 ms.
When I measure the time from outside, like this:
stopwatch.Restart();
// Get data
string[][] data = getDataArray(page.GetTsvText(0));
stopwatch.Stop();
Console.WriteLine(" $$$ $$$ Got data array in: " + stopwatch.ElapsedMilliseconds + " ms");
it takes about 2000 ms.
Does the string initialisation take so long? How can I get it faster, like under 50 ms?
By using Linq
string[][] result = source.Split('\n')
.Select(line => line.Split(new char[] { ' ', ' ' }, StringSplitOptions.RemoveEmptyEntries));
linq has better performance and a faster result.

Is there a way to sort a string with OrderBy while ignoring white spaces?

So, i need to sort the first line from a text file in an ascending order, it goes like this:
32 73 36 3 90 60 27 56 3 19 63 42 47 74 100 56 88 35 83 1 6 17 57 62 21
After it's sorted it should look like:
1 3 3 6 17 19 21 27 32 36 etc.
Im still pretty new at C# so i tried to code it like this:
var lines = System.IO.File.ReadAllLines(#"../../text.txt");
lines[0] = String.Concat(lines[0].OrderBy(x => x));
The output is:
00001111222222
Is there anyway to ignore white spaces and not sort it by single numbers but by two?
23 24 25 instead of 1 1 1 1 2 2 2?
Split the string on spaces, then parse as ints, order, then join e.g. (without error checking):
var lines = System.IO.File.ReadAllLines(#"../../text.txt");
var sorted = lines[0]
.Split()
.Select(x => int.Parse(x))
.OrderBy(x => x);
return string.Join(" ", sorted);

How to access to the result content of javascript request in c# code

I'm trying to parse the results of each runner of Paris marathon
web page.
But, the body of the page is an ajax request.
So, I tried to load the page in a WeBrowser winform component. Even if the the visual render is OK I still cannot find the corresponding HTML result of each runner in the InnerHTM/OutterHTML property of the WebBrowser component.
How could I do :/ Thanks for your help!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.webBrowser1.ObjectForScripting = new MyScript();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.schneiderelectricparismarathon.com/fr/l-evenement/resultats/resultats-marathon");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("javascript: window.external.CallServerSideCode();");
}
[ComVisible(true)]
public class MyScript
{
public void CallServerSideCode()
{
var doc = ((Form1)Application.OpenForms[0]).webBrowser1.Document;
// I cannot find the ranking of each runner in here
}
}
}
See that result is returned in this url
http://www.aso.fr/massevents/resultats/ajax.php?course=mar17&langue=us&version=3&action=search
using an html parser (HtmlAgilityPack)
using (var client = new HttpClient())
{
var html = await client.GetStringAsync("http://www.aso.fr/massevents/resultats/ajax.php?v=1493287097&course=mar17&langue=us&version=3&action=search");
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var details = doc.DocumentNode.SelectSingleNode("//table[#class='footable']")
.Descendants("tr")
.Select(tr => tr.Descendants("td").Select(x => x.InnerText).Take(5).ToList())
.Where(x=>x.Count==5)
.ToList();
}
the printable form of this result is (which is List of List):
1 02:06:10 7 LONYANGATA Paul
2 02:06:57 1 CHEBOGUT Stephen
3 02:07:12 9 YEGO Solomon
4 02:07:19 11 ATNAFU Yitayal
5 02:07:40 4 AYELE Abayneh
6 02:08:25 8 KIPSANG Asbel
7 02:08:37 5 KOSGEI Samuel
8 02:08:44 14 MENGICH Richard
9 02:09:46 91 NKHABUTLANE Motlokoa Clement
10 02:10:02 6 KOGO Micah
11 02:10:11 34 AKOPESHA Abraham
12 02:10:20 19 CHAHDI Hassan
13 02:10:30 13 MUTAI Kipkemei
14 02:10:44 2 KIPTOO Mark
15 02:13:06 16 MALATY Benjamin
16 02:14:07 18 DURAND Yohan
17 02:14:57 24 CHANI Hassan
18 02:17:55 20 LALIRE Paul
19 02:17:55 102 TEFERI Maru
20 02:18:19 101 MORIHASHI Kansuke
21 02:19:01 99 GUNJI Takahiro
22 02:20:11 96 NAVARRO Nicolas
23 02:20:47 140 RANCON Julien
24 02:20:55 205 RIONORIPO Purity
25 02:20:59 204 BARSOSIO Agnes
26 02:21:22 202 CHEYECH Flomena
27 02:21:23 39 MASCIOTRA Julien
28 02:21:36 201 JEPKESHO Visiline
29 02:22:30 108 ROMERO Hugo
30 02:22:51 203 MELESE Yebrgual
31 02:24:23 3066 RAPHAEL Magalhaes Moura
32 02:26:41 105 GUALDI Giovanni
33 02:27:14 111 NEUSCHWANDER Florian
34 02:27:31 90 INI Thierry
35 02:27:52 104 CHAILLOU Thomas
36 02:27:55 5345 HOLLAND Adam
37 02:28:06 991 JEAN Richard
38 02:28:19 6041 JAEN ORTIZ Henrry
39 02:28:36 139 COUZON Clement
40 02:29:07 582 MILANI Massimiliano Andrea
41 02:29:12 3008 RYCKELYNCK Benjamin
42 02:29:13 211 GUDETA Netsanet
43 02:29:21 1104 PURNELLE Alexandre
44 02:30:24 209 KOWALSKA Kataryna
45 02:30:24 857 LUCAS Yoann
46 02:30:32 3967 NEWTON Mark
47 02:30:35 93 BENICHOU Thomas
48 02:30:53 753 TASSAROTTI Luca
49 02:31:01 861 POBLETE Jesus Antonio
50 02:31:25 85 ANFALT Erik

How to create Query syntax for multiple DataTable for implementing IN operator of Sql Server

I have fetched 3-4 tables by executing my stored procedure. Now they resides on my dataset.
I have to maintain this dataset for multiple forms and I am not doing any DML operation on this dataset.
Now this dataset contains 4 tables out of which i have to fetch some records to display data.
Data stored in tables are in form of one to many relationship.
i.e. In case of transactions. N records per record. Then these N records are further mapped to M records of 3rd table.
Table 1
MAP_ID GUEST_ID DEPARTMENT_ID PARENT_ID PREFERENCE_ID
-------------------- -------------------- -------------------- -------------------- --------------------
19 61 1 1 5
14 61 1 5 15
15 61 2 4 10
18 61 2 13 23
17 61 2 20 26
16 61 40 40 41
20 62 1 5 14
21 62 1 5 15
22 62 1 6 16
24 62 2 3 4
23 62 2 4 9
27 62 2 13 23
25 62 2 20 24
26 62 2 20 25
28 63 1 1 5
29 63 1 1 8
34 63 1 5 15
30 63 2 4 10
33 63 2 4 11
31 63 2 13 23
32 63 40 40 41
35 65 1 NULL 1
36 65 1 NULL 1
38 68 2 13 22
37 68 2 20 25
39 68 2 23 27
40 92 1 NULL 1
Table 2
Department_ID Department_Name Parent_Id Parent_Name
-------------------- ----------------------- --------------- ----------------------------------------------------------------------------------
1 Food 1, 5, 6 Food, North Indian, South Indian
2 Lodging 3, 4, 13, 20, 23 Room, Floor, Non Air Conditioned, With Balcony, Without Balcony
40 New 40 SubNew
TABLE 3
Parent_Id Parent_Name Preference_ID Preference_Name
-------------------- ----------------------------------------------- ----------------------- -------------------
NULL NULL NULL NULL
1 Food 5, 8 North Indian, Italian
3 Room 4 Floor
4 Floor 9, 10, 11 First, Second, Third
5 North Indian 14, 15 X, Y
6 South Indian 16 Dosa
13 Non Air Conditioned 22, 23 With Balcony, Without Balcony
20 With Balcony 24, 25, 26 Mountain View, Ocean View, Garden View
23 Without Balcony 27 Mountain View
40 New 41 SubNew
I have these 3 tables that are related in some fashion like this.
Table 1 will be the master for these 2 tables i.e. table 2 and table 3.
I need to query on them as
SELECT Department_Id, Department_Name, Parent_Name FROM Table2 WHERE Department_Id in
(
SELECT Department_Id FROM Table1 WHERE guest_id=65
)
SELECT Parent_Id, Parent_Name, Preference_Name FROM Table3 WHERE PARENT_ID in
(
SELECT parent_id FROM Table1 WHERE guest_id=65
)
Now I need to use these queries on DataTables.
So I am using Query Syntax for this and reached up to this point.
var dept_list= from dept in DtMapGuestDepartment.AsEnumerable()
where dept.Field<long>("PK_GUEST_ID")==long.Parse(63)
select dept;
This should give me the list of all departments that has guest id =63
Now I want to select all departments_name and parent_name from Table 2 where guest_id=63 i.e. departments that i fetched above.
This same case will be followed for Table3.
Please suggest how to do this.
Thanks for keeping up patience for reading my question.
Well, suppose you have 3 IEnumerable vars, one for each table. Your first query would be like this:
var records = from r1 in table1 where r1.GuestId == 63
join r2 in table2 on r1.DepartmentId equals r2.DepartmentId
select r2;
The second query is nearly the same as the first one, joining with table 3 instead of 2.
var records = from r1 in table1 where r1.GuestId == 63
join r3 in table3 on r1.ParentId equals r2.ParentId
select r3;

Categories