Results 1 to 11 of 11

Thread: difference in string lengths

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    difference in string lengths

    For the life of me I can't remember this.

    I want to find out the difference in length between 2 strings.

    but really this is what I am trying to achieve:

    to get the length from stringA to stringB - so everything in between these 2 strings, I want to take.

    any ideas?

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: difference in string lengths

    This shows the difference in lengh between 2 strings
    Code:
    string a = "Holapapotatop";
    string b = "Holapa";
    MessageBox.Show ((a.Length - b.Length).ToString());
    This one returns the absolute value
    Code:
    string a = "Holapapotato77p";
    string b = "Holapa";
    MessageBox.Show (string.CompareOrdinal(a,1,b,1,100).ToString());
    Last edited by jcis; Feb 28th, 2006 at 11:02 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference in string lengths

    Quote Originally Posted by Techno
    to get the length from stringA to stringB - so everything in between these 2 strings, I want to take.
    Huh? Are you after a number or a substring? Also, I think may have misunderstood what CompareOrdinal does jcis. It will return -1, 0, or 1 depending on the order of the two strings based on Unicode values rather than alphabetically as Compare does.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: difference in string lengths

    Quote Originally Posted by jmcilhinney
    It will return -1, 0, or 1 depending on the order of the two strings based on Unicode values rather than alphabetically as Compare does.
    Actually it returns 9 and that's the diference in lengh between both strings,
    Why you say its just -1,0,1?

    EDIT: But now I see this just works when a contains b, or b contains a.
    Last edited by jcis; Mar 1st, 2006 at 12:39 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference in string lengths

    I was mistaken to say specifically -1 and 1. It actually returns a value less than zero, zero or a value greater than zero. This value is not supposed to indicate anything about the relative lengths of the two strings, but rhather there relative order when comapison is based on Unicode value rather than "alphabetic". I'm still confused as to what Techno meant by that line I quoted. I thought may be he meant if one string is n characters long and the other is m characters long then return the last abs(n-m) characters of the longer string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: difference in string lengths

    I wish people would articulate their questions properly.
    I don't live here any more.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: difference in string lengths

    lol sorry, it was 5 am :|

    I want to get the STRING between stringA and stringB

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: difference in string lengths

    Quote Originally Posted by Techno
    lol sorry, it was 5 am :|
    I want to get the STRING between stringA and stringB
    you mean both are part of a single string?, like..

    "Hi, how are you"

    You want to get a substring given 2 strings, i.e. given "Hi" and "you", the result would be ", how are ", is this ok?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference in string lengths

    I'm still confused too. There are no strings between two string unless you mean that they are both substrings of a third, as jcis suggests. Maybe an example is in order.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: difference in string lengths

    what jcis said is correct -

    hi, how are you

    I want a string from "hi" to "you" so:

    , how are

    would be the result im after

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference in string lengths

    Code:
    string full = "Hi. how are you?";
    string first = "Hi";
    string second = "you";
    
    int start = full.IndexOf(first) + first.Length;
    int length = full.IndexOf(second) - start;
    
    string middle = full.Substring(start, length);
    You may want to use LastIndexOf for the second substring, although if there is only one instance it won't matter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width