2

I tried to split a string wich contains these character #

domicilioSeparado = domicilio.Split(@"#".ToCharArray());

but every time the array contains just one member. I've tried a lot of combinations but anything seems to work, I also tried to replace the string with a blank space and it kinda works - the problem is that it remains a single string.

domicilio = domicilio.Replace(@"#", @" ");

How can I resolve this?

Complete code:

String[] domicilioSeparado;
String domicilio = dbRow["DOMICILIO"].ToString();

domicilioSeparado = domicilio.Split(@"#".ToCharArray());
if (Regex.IsMatch(domicilioSeparado.Last(), @"\d"))
{
    String domicilioSinNum = "";
    domicilioSinNum = domicilioSeparado[0];
    custTable.Rows.Add(counter, dbRow["CUENTA"], nombre,
        paterno, materno, domicilioSinNum, domicilioSeparado.Last(), tipoEntidad);
} 
12
  • 8
    What's your string? The first example should work just fine. (Although '#' would be a lot better than @"#".ToCharArray().) Commented Aug 9, 2012 at 18:35
  • 3
    Split with a '#' character should work just fine. Could you provide the input string you are using? Commented Aug 9, 2012 at 18:35
  • 1
    You realize you can simply do domicilio.Split('#')? Commented Aug 9, 2012 at 18:37
  • 1
    I don't suppose the characters are music sharp signs (U+266F) ♯ and not a standard number sign #? Commented Aug 9, 2012 at 18:37
  • 1
    @vcsjones: Not likely since the name domicilio implies an address of some sort. Commented Aug 9, 2012 at 18:40

2 Answers 2

6

If you just want to split a string on a delimiter, in this instance '#', then you can use this:

domicilioSeparado = domicilio.Split("#");

That should give you what you want. Your second attempt simply replaces all the characters '#' in the string with ' ', which doesn't seem to be what you want. Can we see the string you're trying to split? That might help explain why it's not working.

EDIT:

Ok, here's how I think your code should look, give this a shot and let me know how it goes.

List<string> domicilioSeparado = new List<string>();
String domicilio = dbRow["DOMICILIO"].ToString();

domicilioSeparado = domicilio.Split("#");

if (Regex.IsMatch(domicilioSeparado.Last(), @"\d"))
{
    String domicilioSinNum = "";
    domicilioSinNum = domicilioSeparado[0];
    custTable.Rows.Add(counter, dbRow["CUENTA"], nombre,
        paterno, materno, domicilioSinNum, domicilioSeparado.Last(), tipoEntidad);
} 
Sign up to request clarification or add additional context in comments.

17 Comments

PRIVADA#9, it seems to split, because I print #9 but I cannot print PRIVADA alone
What does domcilioSeparado actually contain after that statement, though?
String domicilio = dbRow["DOMICILIO"].ToString(); domicilioSeparado = domicilio.Split(" ".ToCharArray());
@user1588438 The array starts at 0 buy the way.
Well, I found the mistake, I was printing the whole string cause I didn't quit the last split, sorry for the troubles, thanks a lot my friend...
|
1

Try this:

string[] domicilioSeparado;
domicilioSeparado = domicilio.Split('#');

Some notes: 1 - It is ('#'), instead of ("#"); 2 - Replace does not split a string, it only replace that part, keeping as a single string.

In case you want an example that includes the printing of the whole array:

string domicilio = "abc#def#ghi";
string[] domicilioSeparado;
domicilioSeparado = domicilio.Split('#');
for (int i = 0; i < domicilioSeparado.Length; i++)
{
   MessageBox.Show(domicilioSeparado[i]);
}

It will open a Message Box for each element within domicilioSeparado.

8 Comments

So, I won't have an array of strings?
Yes, you will. If you print domicilioSeparado[1], for instance, you will get the second part of domicilio.
Cause I've done that, i didn't put the declaration of the string, but I think I could print domicilioSperadao[0] or domicilioSperadao[n], is it possible?
Well, then my problem isn't the split, my problem is to print just the first part of the string, insted it prints the complete string
Well, I found the mistake, I was printing the whole string cause I didn't quit the last split, sorry for the troubles, thanks a lot my friend...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.