working with strings 
Author Message
 working with strings

I have to take a string and split it into two seperate
strings.  The first string has one word, then a hyphen,
then the second word.  I need to find the hyphen, and then
put everything before the hyphen in one string and
everything after the hyphen in a second.  I know in
VB.NET, String.Split("-") would work for this.  Does
anyone know how to do this in C#?

Thanks!



Sat, 20 Nov 2004 03:10:47 GMT  
 working with strings
Jason, here's one way using Split:

string s = "Hello-There";
string[] word = s.Split('-');
for (int i = 0; i < word.Length; i++)
 Console.WriteLine("Word #{0}: \"{1}\".", i, word[i]);

-glenn-


Quote:
> I have to take a string and split it into two seperate
> strings.  The first string has one word, then a hyphen,
> then the second word.  I need to find the hyphen, and then
> put everything before the hyphen in one string and
> everything after the hyphen in a second.  I know in
> VB.NET, String.Split("-") would work for this.  Does
> anyone know how to do this in C#?

> Thanks!



Sat, 20 Nov 2004 03:25:16 GMT  
 working with strings
Jason, you can use the same function in C#.  Here's some sample code which
creates an array of strings from the initial string.

string str = "test-test";

string[] str2 = str.Split(new char[] {'-'}, 2);

--
Greg
http://www.claritycon.com/


Quote:
> I have to take a string and split it into two seperate
> strings.  The first string has one word, then a hyphen,
> then the second word.  I need to find the hyphen, and then
> put everything before the hyphen in one string and
> everything after the hyphen in a second.  I know in
> VB.NET, String.Split("-") would work for this.  Does
> anyone know how to do this in C#?

> Thanks!



Sat, 20 Nov 2004 03:30:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. working with strings

2. Working with strings

3. Newbie working with strings

4. automaticly showing methods (works for string, not stringstream)

5. String comparation not working

6. String copying not working?

7. Why Doesn't this Simple String Work???

8. COM Dates - how to work out how to display as a string

9. Setting not Null field to ""(empty string) does not work with CRecordset

10. String Class of C++ Works in VC++?

11. How does STL string class works!

12. Working with latest MFC version and string class

 

 
Powered by phpBB® Forum Software