Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.00/5 (1 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:

C#
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
Updated 5-Oct-12 12:15pm
v2
Comments
[no name] 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.

This worked for me.

C#
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)
            {
                MessageBox.Show(y);
            }
 
Share this answer
 
Hi,

Here is the improved version of Solution1,
C#
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,
Thanks
 
Share this answer
 
This will split the string into 2 parts on the first period but not the second:

C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900