Click here to Skip to main content
13,152,454 members (38,600 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone,

I am using the String split method for the first time and ran into a little bit of an issue. I have some file names that have more than 1 of the same special character in them. How is it possible to have the splitter ignore the 2nd of the 2?

This is what I have so far:

string cc_key_numbers = cc_key_number_dr.GetString(0);
string[] cc_key_number_split = cc_key_numbers.Split(new Char[] { ' ', ',' });
foreach (string c in cc_key_number_split)
        if (c.Trim() != "")
              SqlCeCommand cc_waveform_comms = new SqlCeCommand("SELECT file_name FROM waveform_files WHERE [key] = " + c, conn);
                        cc_waveform_comms.CommandType = CommandType.Text;
                        SqlCeDataReader cc_waveform_comms_dr = cc_waveform_comms.ExecuteReader();
               if (cc_waveform_comms_dr.Read())
                            string cc_file_names = cc_waveform_comms_dr.GetString(0);
                            string[] cc_file_name_split = cc_file_names.Split(new Char[] { '.' });
                            foreach (string cc_file in cc_file_name_split)
                                char cc_file_char = cc_file[0];
                                if (!Char.IsNumber(cc_file_char))
                                    writer.WriteElementString("caption", cc_file);
                                else { }
                            writer.WriteElementString("picoscopefile", "C:\\Pico Waveforms\\" + cc_waveform_comms_dr.GetString(0) + ".psdata");

I have a file name with the example of:
15.Crank vs.Cams Bank 2

I want to trash the first . and everything before and keep the rest (including the 2nd .).

What the above code outputs is this:
Crank vs
Cams Bank 2

What I want to output is this:
Crank vs. Cams Bank 2

Thanks everyone!
Posted 5-Oct-12 10:45am
Updated 5-Oct-12 12:15pm
Wes Aday 5-Oct-12 16:51pm
If your string is always in that format, I would suggest that you just get the index of the first . and then remove everything up to (and including the .) leaving the string you want.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This worked for me.

q = "15.Crank vs.Cams Bank 2"; // The variable to split.
            string s= q.Substring(txtsplit.Text.IndexOf('.')); //Get the text after the first '.'
            string[] v = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string y in v)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Here is the improved version of Solution1,
string result;
string fileName = "15.Crank vs.Cams Bank 2"; // The variable to split.
string[] v = fileName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
for(i =1; i<v.length;i++)>
 result += v[i];

Here in for loop i have ignore first value and then append rest of the value. another solution is to remove first value from result array and then use join to combine all other values.

Hope it works for you,
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This will split the string into 2 parts on the first period but not the second:

string test = "15.Crank vs.Cams Bank 2";
string[] allTests = test.Split(new string[] { "." }, 2, StringSplitOptions.RemoveEmptyEntries);

As a side note, you may want to look into what SQL injection is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170924.2 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100