Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# strings
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
Edited 5-Oct-12 12:15pm
v2
Comments
Wes Aday at 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
good
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)
            {
                MessageBox.Show(y);
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
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,
Thanks
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 646
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
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