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?
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());
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? :confused: 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.
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.
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.
Re: difference in string lengths
I wish people would articulate their questions properly. :rolleyes:
Re: difference in string lengths
lol sorry, it was 5 am :|
I want to get the STRING between stringA and stringB
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?
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.
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
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.