none
[UWP][C#] How to see what a string contains with certain length RRS feed

  • Question

  • Hi all -

    I have somewhat of a unique request. An example would be this: I have a string that says "ThisIsAString" and I want to know what the text says from the 5th letter to the 14th letter. Anyway to get this length?

    Any help is appreciated

    Thanks!


    Zack Bowling



    Tuesday, October 6, 2015 12:45 AM

Answers

  • In the following case the string "ThisIsAString" only contains 13 characters, so the below code will fail, however if the string had 14 or more characters it would handle your scenario

    string s = "123ThisIsAString456";
    var result = s.Substring(5, 14);

    The above code positions the cursor just after the 5th character, or another way of saying this is that it gets the 5th index in the 0-based array, and then counts 14 characters from that 5th index. This result contains the value "isIsAString456". Notice the 14 characters are the length to count from the 5th index.

    Now if you want the 5th to the 14th index you'd do something like

    //to get 5th index to 14th index
    var data = s.Substring(5, 10);

     This gets the 5th to the (+10 char positions = 14th) index based on a Zero based index.

    HTH


    Sr. Enterprise Architect | Trainer | Consultant | MCT | MCSD | MCPD | SharePoint TS | MS Virtual TS |Windows 8 App Store Developer | Linux Gentoo Geek | Raspberry Pi Owner | Micro .Net Developer | Kinect For Windows Device Developer |blog: http://dgoins.wordpress.com




    Tuesday, October 6, 2015 2:00 AM

All replies

  • What language? The exact syntax will be language dependent. Please tag your posts per Guide to posting: subject line tags

    Tuesday, October 6, 2015 1:17 AM
  • What language? The exact syntax will be language dependent. Please tag your posts per Guide to posting: subject line tags

    Sorry, I meant to post it in the title. It is C#

    Zack Bowling

    Tuesday, October 6, 2015 1:49 AM
  • In the following case the string "ThisIsAString" only contains 13 characters, so the below code will fail, however if the string had 14 or more characters it would handle your scenario

    string s = "123ThisIsAString456";
    var result = s.Substring(5, 14);

    The above code positions the cursor just after the 5th character, or another way of saying this is that it gets the 5th index in the 0-based array, and then counts 14 characters from that 5th index. This result contains the value "isIsAString456". Notice the 14 characters are the length to count from the 5th index.

    Now if you want the 5th to the 14th index you'd do something like

    //to get 5th index to 14th index
    var data = s.Substring(5, 10);

     This gets the 5th to the (+10 char positions = 14th) index based on a Zero based index.

    HTH


    Sr. Enterprise Architect | Trainer | Consultant | MCT | MCSD | MCPD | SharePoint TS | MS Virtual TS |Windows 8 App Store Developer | Linux Gentoo Geek | Raspberry Pi Owner | Micro .Net Developer | Kinect For Windows Device Developer |blog: http://dgoins.wordpress.com




    Tuesday, October 6, 2015 2:00 AM
  • Very nice! Thank you very much, this is exactly what I was looking for.

    Thanks!


    Zack Bowling

    Tuesday, October 6, 2015 2:54 AM