How do I read and parse data from individual rows? [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am new at C#.net. Could somebody help me with the following issue? Thank you.
I need to read the content from a file, then check each row of the file for data that are separated by ":" or ",". Then get the data that is between ":" and ",". Finally add it to the datatable.
How do I do this?
Any help is highly appreciated.

String linestring = streamreader.ReadLine();
String[] linetokens = linestring.Split(new String[]{":",","}, StringSplitOptions.None);
After that linetokens array will be filled with the segments you need.

Given the generality and overall scope of your question (ie, you should break it up into the parts you don't understand and ask them individually), this is best I could come up with that could do what you want.
var data = File.ReadLines() // read the content from a file
.Where(line => line.Contains(":") && line.Contains(",") // data separated by ":" & ","
.Select(line => line.Split(":,".ToArray())[1]) // data between ":" & "," -- could yield data between "," and ":"
.Select(data => new object[] {data}); // for DataTable.Rows.Add
// I can only assume you have a DataTable with one column
foreach(var rowData in data)
yourTable.Rows.Add(rowData);
Hopefully this inspires you.

Related

res.Text = "\n\n" + Regex.Split("THE THEVA RESIDENCY 0|Boutique Hotels 1|","|")[1];Regex doing terrible [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
res.Text = "\n\n" + Regex.Split("THE THEVA RESIDENCY 0|Boutique Hotels 1|","|")[1];
This gives me only the first string of the First items "T"...
I am assuming that you want to split the string with | as the delimiter. To do this use [|]. Change the regex pattern parameter from "|" to "[|]".
res.Text = "\n\n" + Regex.Split("THE THEVA RESIDENCY 0|Boutique Hotels 1|","[|]")[1];
The reason why | did not work because on its own it is a Alternation Construct. See http://msdn.microsoft.com/en-us/library/az24scfc.aspx#alternation_constructs
If your goal is to extract the substring "Boutique Hotels 1" then perhaps you want
res.Text = "\n\n" + "THE THEVA RESIDENCY 0|Boutique Hotels 1|".Split('|')[1];

string similarity and pattern matching [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Say I have strings like these
Sam is Boy
Joseph is Boy
Jasmine is Girl
Annie is Girl
Chris is Boy
I have a quick and murky way of preparing a C# Dictionary like this..!
input.ForEach(i =>
{
string[] values = i.Split();
input_dictionary.Add(values[0], values[2]);
});
Do we have any other better/optimised way of achieving this, since the input data follows a fixed format like "Name is Gender"?
Here's a regex pattern you could use:
(.+) is (Boy|Girl)
The following is probably faster, but you should test it.
input_dictionary = (from i in input
let n = i.IndexOf(' is ')
select new { Name = i.Substring(0, n), Sex = i.Substring(n + 4) }
).ToDictionary(i => i.Name, i => i.Sex);
You can also the Regex class, which might be faster or slower than the above. It's difficult to say without testing.

Remove last character in a list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am building a list from a dataset as 12345_9876,125675_0987,
I try to remove the last , from the list like we do for strings..
value.TrimEnd(',');
Can we do similar thing for a list?
I hope this will help you?
var lastElement = yourList.ElementAt<string>(yourList.Count - 1).TrimEnd(',');
yourList.RemoveAt(yourList.Count - 1);
yourList.Insert(yourList.Count,lastElement);
Think you just want this?:
List<string mylistfromdataset = // I dunno how you populated it
var newlist = mylistfromdataset.Select(x=> x.TrimEnd(',')).ToList();
EDIT after the poster cleared the question, you could use this 2 line code to make a new list where the last string the , is trimmed.
var list = listofstring.Take(listofstring.Count - 1).ToList();
list.Add(listofstring.Last().TrimEnd(','));
(Make a list excluding the last item. then trim the last item with , at the end and at it to the new list.)
Just use String.Join:
String delimitedList = String.Join(",", yourList);
int i=value.LastIndexOf(",");
if(i!=-1)
value=value.Remove(i,1);

How to retrieve data between two <br/> tags [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to get the data between the two <br/> tags.
The data in xml file is like this <br/>lots of data<br/>.
Now I want to get the data between the two <br/> tags and fetch specific data in it.
Can anyone suggest me how to search the tags and fetch the specific data between them?
Well you could use some LINQ to XML here to do this.
Example:
var xmlStr = #"<root>
data1<br/>
data2<br/>
data3<br/>
data4<br/>
data5<br/>
</root>";
var doc = XDocument.Parse(xmlStr);
var query =
from br in doc.Descendants("br")
let textNode = br.NextNode as XText
where textNode != null
let nextBr = textNode.NextNode as XElement
where nextBr != null && nextBr.Name == "br"
select textNode.Value;
LINQ to XML approach is fairly more approachable than this as Jeff Mercado's answer. As using RegEx is not recommended for parsing XML data. But if your requirement is for one time only then it might help.
(?is)(?<=^|<br/>).*?(?=<br/>)

Howto split genericList items with delimiter? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I cant figure out howto split items from my genericList to two seperate parts with delimiter option?
List<string> fileLines = File.ReadAllLines(fileName).Skip(4).ToList();
foreach (var item in fileLines)
{
values = item.Split(' ');
sList.Add(values[3].Substring(2).Trim());
}
My sList looks like this:
10.5 5.5
7.2 2.5
-0.1 3.0
-1.1 3.3
and so on .......... totaly 8760 rows in my List.
What I want to do is to split each row from the List to two seperate parts so I can count the min, max and average on thoose values.
(each value is meant to represent the temperature, so double)
Any help would be appreciated !!! Thanx
So why don't you use this one
List<string> fileLines = File.ReadAllLines(fileName).Skip(4).ToList();
foreach (var item in fileLines)
{
values = item.Split(' ');
string[] vl=values[3].Substring(2).Trim().Split('\t');
sList1.Add(vl[0]);
sList2.Add(vl[1]);
}

Categories

Resources