End of the line


#1

Hello,

is there a way how to go or find the end of the row? Lets say I have an array strings and I need to check that it ends by a number.

I understand that I can find the lenght of the string so I have index of last symbol in the streng. However, I need to know what is the last word, which means that I need to find last space before the last word.

Thanks a lot.


#2

Hi,
string[] stringArray = { “one”, “two”, “three”, “four” };
var last=stringArray.Last();


#3

You can do this with regular expressions, but I don’t understand your criteria clearly enough. If you just need to verify that the very last character of the string is a digit, use \d$. This is a very short pattern, and as always there are subtle caveats that require more knowledge of the context:

  • $ matches only the end of the whole string, unless you enable Multiline, in which case it matches the end of each line;
  • In that case it may also be preceded by invisible characters like \r (carriage return), requiring you to write something like \d\r?$ to avoid falsely rejecting matches.

This will still only give you a single digit or nothing. If you need to extract the whole number for processing instead of just finding a digit at the end, use \d+ instead of \d to capture consecutive digits.


#4

To be more specific:

I have a string on each row of DT and I have to loop through it.
I need to know which number take into account based on the beginning of a row.

So I have e.g.:
Recept bla bla bla 20,44 30,42
Tree blabbla bla 20,33
and other rows which are not ended by a number.

So I need to find every row with number in the end and take them into account if in the beginning is Tree.We have also find this command: textstring.Split(" ").Last
Should not it work?


#5

Well, you may be able to use both approaches (string operations and regex) in this case. Indeed you can use String.Split and then as @ddpadil advises access the required elements from the resulting array.

You can do the same (and much more) with regular expressions, for example:

  • (?<=^Tree.*)\d+,\d+\r?$: Match two groups of digits separated by a comma (and the optional carriage return) at the end of a line that started with “Tree”. The word Tree and the rest of the line (.*) are not included in the match.
  • ^(\w+)\b.*\b(\d+,\d+)\r?$: Match any word at the start of a string that ends in the same number pattern. This time, both the word and number are in “capturing groups”, which makes it easy to extract them from the entire match: the word will be in match.Groups(1).Value and the number in match.Groups(2).Value.

Which of these (or other) examples would be best is up to you. Regexes are extremely powerful compared to string operations, but also complicated and perhaps overpowered. On the other hand, sometimes they are just the most direct way of extracting multiple bits of information from a string.


#6

Are you wanting all the rows that meet your criteria of starting with Tree and ending with a number? You could do this by using the Where and Select functions from the datatable which will create an array of rows which you can then process.

with dt as your datatable variable:
dt.AsEnumerable().Where(Function(row) row(0).ToString.Trim.StartsWith(“Tree”) and IsNumeric(Right(row(0).ToString.Trim,1))).Select(Function(row) row).ToArray()
or
(From row In dt.AsEnumerable() Where row(0).ToString.Trim.StartsWith(“Tree”) and IsNumeric(Right(row(0).ToString.Trim,1)) Select row).ToArray()

Instead of Right() you could split by space too like IsNumeric(row(0).Split(" “c)(row(0).Split(” "c).Count-1).ToString)

if .Last works then you could use that instead of .Count-1

I’m not 100% sure on the syntax.

That will return an array of DataRows which you can adjust the values of or run through a process.