1

I have list of strings - sList with numeric values (10,20...) and I want to find out number of it -> 10 returns 2.

I tried sList[i].Length but it didn't work.

TonySixx
  • 59
  • 4
  • 2
    Could you please be more clear? How the list looks like? are you able to show some code that you are using? – sujith karivelil Aug 24 '16 at 07:54
  • You mean you want it to return 2 as that's the length of the string or you want it to return the actual value e.g. 10 in this case? – sr28 Aug 24 '16 at 07:55
  • 1
    Which string in the list do you want the length of? All the strings, just one element, ? – Murray Foxcroft Aug 24 '16 at 07:55
  • 1
    [How to create a Minimal, Complete and Verifiable example.](http://stackoverflow.com/help/mcve) – Matthew Watson Aug 24 '16 at 07:56
  • @un-lucky `string[] s = this.Kod.Split('.');` -> values like 10.4.20 so I split It to array with values 10,4,20 than 'var sList = s.ToList();' so I convert it to list with strings – TonySixx Aug 24 '16 at 07:59
  • @sr28 I want it to return 2 as that's length of the string – TonySixx Aug 24 '16 at 08:00
  • @TonySixx - what do you mean doesn't work? Please give sample data and expected result for that data – Gilad Green Aug 24 '16 at 08:00
  • 1
    @TonySixx sList[i].ToString().Length – Maverick Aug 24 '16 at 08:00
  • If sList is a list of strings then .Length should work. Please show the code for the creation and filling of sList. – sr28 Aug 24 '16 at 08:06
  • 1
    @Maverick It seems that works, thanks:) – TonySixx Aug 24 '16 at 08:07
  • Do you mean `2` is the *max* list item length? – Wiktor Stribiżew Aug 24 '16 at 08:07
  • @WiktorStribiżew No, if there is value "100" I want to return 3, if value "20" I want return 2 and so on.. I want return length of string – TonySixx Aug 24 '16 at 08:08
  • 1
    Then it is such a basic question, you could easily check it at MSDN. `String.Length`. – Wiktor Stribiżew Aug 24 '16 at 08:09
  • @WiktorStribiżew I've checked it but they have example: `string characters = "abc\u0000def";` `Console.WriteLine(characters.Length); // Displays 7` So it count only letters in string and I want to count numbers too. – TonySixx Aug 24 '16 at 08:12
  • @TonySixx: The `\u0000` is **1 `null` character**. The string has no numbers. See https://ideone.com/CC0mRW. See [MSDN explanation](https://msdn.microsoft.com/library/system.string.length(v=vs.110).aspx): *The Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.* – Wiktor Stribiżew Aug 24 '16 at 08:13
  • @WiktorStribiżew aha okay thanks for clarification – TonySixx Aug 24 '16 at 08:15

1 Answers1

2

If the list is a list of strings then .Length should work. Otherwise you can try this.

sList[i].ToString().Length
Maverick
  • 1,396
  • 5
  • 22
  • 42