|
-
Feb 28th, 2006, 10:39 PM
#1
Thread Starter
PowerPoster
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?
-
Feb 28th, 2006, 10:56 PM
#2
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.
-
Mar 1st, 2006, 12:29 AM
#3
Re: difference in string lengths
 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.
-
Mar 1st, 2006, 12:34 AM
#4
Re: difference in string lengths
 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.
-
Mar 1st, 2006, 12:52 AM
#5
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.
-
Mar 1st, 2006, 07:46 AM
#6
Re: difference in string lengths
I wish people would articulate their questions properly.
I don't live here any more.
-
Mar 1st, 2006, 08:25 AM
#7
Thread Starter
PowerPoster
Re: difference in string lengths
lol sorry, it was 5 am :|
I want to get the STRING between stringA and stringB
-
Mar 1st, 2006, 08:44 AM
#8
Re: difference in string lengths
 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?
-
Mar 1st, 2006, 10:29 PM
#9
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.
-
Mar 2nd, 2006, 01:58 PM
#10
Thread Starter
PowerPoster
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
-
Mar 2nd, 2006, 06:44 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|