My xpath need to return a link that is inside an array, but when I try to select the link inside the array using [1] the complete array is returned.
This is my array li7 = [a, a.link] and I'm trying to get li7[1], but my return is [a,a.link]
When I try to see what is inside my first element of the array (li7[0]) an empty array is returned.
I don't know what to do anymore
While XPath 3.1 has arrays, it is not clear whether and how you use try to use them. Consider to both show the input XML and the C# code that fails. In XPath 3.1, an array is a function, you access the first item with e.g. $a(1) or $a?1 but not with $a[1], the positional predicate is what you use in XPath for node-sets (XPath 1) or sequences of items (XPath 2 and later). Whatever your XPath expression is, if exp selects those two elements and you want only a single one use (exp)[1].
I'm trying to use Linq to determine if a string is NOT in an array. The code I'm using is:
if (!stringArray.Any(soughtString.Contains)){
doStuff();}
but it's not working. Obviously creating a foreach loop would suffice, but I'd like to understand why this line isn't working. And yes, the file has using System.Linq;
You're not asking if the string is not in the array, you're asking if none of the strings in the array are sub-strings in some other string. Apparently at least one is, even though it's not equal.
You just want to do a simple Contains check:
if(!stringArray.Contains(soughtString))
You are currently passing the "Any" function the "Contains" method (which is then being passed each string in the array). In other, words:
array.Any(s => soughtString.Contains(s));
Likely, you want it the other way:
array.Any(s => s.Contains(soughtString));
I'm trying to get the first occurrence in my substring start point:
string dir = Request.MapPath(Request.ApplicationPath) + "\\App_GlobalResources\\";
foreach (var file in Directory.EnumerateFiles(dir, "*.resx"))
{
ddlResources.Items.Add(new ListItem { Text = file.Substring(firstoccuranceof("."), file.LastIndexOf(".")), Value = file });
}
if I do file.Substring(file.IndexOf("."), file.LastIndexOf(".")) I get an error
To answer your actual question - you can use string.IndexOf to get the first occurrence of a character. Note that you'll need to subtract this value from your LastIndexOf call, since Substring's second parameter is the number of characters to fetch, not a start and end index.
However... Instead of parsing the names, you can just use Path.GetFilenameWithoutExtension to get the filename directly.
First occurence
String.IndexOf('.')
Last occurence
String.LastIndexOf('.')
Use IndexOf and LastIndexOf string methods to get index of first and last occurrence of "search" string. You may use System.IO.Path.GetExtension(), System.IO.Path.GetFileNameWithoutExtension(), and System.IO.Path.GetDirectoryName() methods to parse the path.
For instance,
string file = #"c:\csnet\info.sample.txt";
Console.WriteLine(System.IO.Path.GetDirectoryName(file)); //c:\csnet
Console.WriteLine(System.IO.Path.GetFileName(file)); //info.sample.txt
Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file));//info.sample
Console.WriteLine(System.IO.Path.GetExtension(file)); //.txt
file.IndexOf(".")
Should get you the first occurence of ".". Otherwise it will return -1 if not found.
I think that in your particular case you are NOT trying to get IndexOf... Instead you need to use 0 because you are trying to create a key based on filename if understand correctly:
`ddlResources.Items.Add(new ListItem(file.Substring(0, file.LastIndexOf(".")), file ));`
Also, you have '{}' in there as in new ListItem { ... } which is also going to cause a syntax error... Anyhow have a look..
Because the original question is marked with the [regex] tag, I'll provide the following solution, however the best answer for simple parsing of paths using .NET is not by regex.
//extracts "filename" from "filename.resx"
string name = Regex.Match("filename.resx", #"^(.*)\..+?$").Groups[1].Value;
Use an answer that relies on the Path class instead, for simplicity. Other answers contain that info.
Here you go!)
Using a string variable type
int index = str.IndexOf(#"\");
"C:\Users\somebody\Desktop\aFolder\someFile"
https://www.peachpit.com/articles/article.aspx?p=31938&seqNum=12#:~:text=To%20find%20the%20first%20or,string%20you%20are%20searching%20for.
Working on a program that takes a CSV file and splits on each ",". The issue I have is there are thousand separators in some of the numbers. In the CSV file, the numbers render correctly. When viewed as a text document, they are shown like below:
Dog,Cat,100,100,Fish
In a CSV file, there are four cells, with the values "Dog", "Cat", "100,000", "Fish". When I split on the "," to an array of strings, it contains 5 elements, when what I want is 4. Anyone know a way to work around this?
Thanks
There are two common mistakes made when reading csv code: using a split() function and using regular expressions. Both approaches are wrong, in that they are prone to corner cases such as yours and slower than they could be.
Instead, use a dedicated parser such as Microsoft.VisualBasic.TextFieldParser, CodeProject's FastCSV or Linq2csv, or my own implemention here on Stack Overflow.
Typically, CSV files would wrap these elements in quotes, causing your line to be displayed as:
Dog,Cat,"100,100",Fish
This would parse correctly (if using a reasonable method, ie: the TextFieldParser class or a 3rd party library), and avoid this issue.
I would consider your file as an error case - and would try to correct the issue on the generation side.
That being said, if that is not possible, you will need to have more information about the data structure in the file to correct this. For example, in this case, you know you should have 4 elements - if you find five, you may need to merge back together the 3rd and 4th, since those two represent the only number within the line.
This is not possible in a general case, however - for example, take the following:
100,100,100
If that is 2 numbers, should it be 100100, 100, or should it be 100, 100100? There is no way to determine this without more information.
you might want to have a look at the free opensource project FileHelpers. If you MUST use your own code, here is a primer on the CSV "standard" format
well you could always split on ("\",\"") and then trim the first and last element.
But I would look into regular expressions that match elements with in "".
Don't just split on the , split on ", ".
Better still, use a CSV library from google or codeplex etc
Reading a CSV file in .NET?
You may be able to use Regex.Replace to get rid of specifically the third comma as per below before parsing?
Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
[C#] public string Replace(string, MatchEvaluator, int, int);
I ran into a similar issue with fields with line feeds in. Im not convinced this is elegant, but... For mine I basically chopped mine into lines, then if the line didnt start with a text delimeter, I appended it to the line above.
You could try something like this : Step through each field, if the field has an end text delimeter, move to the next, if not, grab the next field, appaend it, rince and repeat till you do have an end delimeter (allows for 1,000,000,000 etc) ..
(Im caffeine deprived, and hungry, I did write some code but it was so ugly, I didnt even post it)
Do you know that it will always contain exactly four columns? If so, this quick-and-dirty LINQ code would work:
string[] elements = line.Split(',');
string element1 = elements.ElementAt(0);
string element2 = elements.ElementAt(1);
// Exclude the first two elements and the last element.
var element3parts = elements.Skip(2).Take(elements.Count() - 3);
int element3 = Convert.ToInt32(string.Join("",element3parts));
string element4 = elements.Last();
Not elegant, but it works.
I have a text file that I wish to read and split into words.
I have a RichTextBox that I will get the text from and split into words also.
I want to be able to compare the words in the two arrays and then display a message.
How can I do this in c#?
You can put words that you get from the text file into a HashSet and the ones you get from the ricktextbox1 to another HashSet and intersect them.
var set1 = new HashSet<string>(words1);
var set2 = new HashSet<string>(words2);
set1.IntersectWith(set2);
By the way, HashSet class is introduced with .Net 3.5
HashSet
You can always go with the brute force, going through the words from one array and checking if they are in the other. For every string you find in both arrays you mark the string or copy it into a separate array for strings that are in both of the arrays. When you finish you just print/count the strings in the new array.
Please note that this is very a slow approach and there are better ways of doing this out there, however you didn't mention speed was of the essence, and this will get you there.
You could try doing this with a HashSet (as Josh Einstein recommended), I am not familiar with that collection but a quick search tells me that it could work very well.