Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

The Listbox items having following formats

" -rwxrwxrwx 1 owner group 3233 Jul 27 18:28 1.log "

from that item i need to get or select date, time and file name (ex jul27 18:20 and 1.log for file) from the listbox and assing to local variables. I am trying some of the way but cant get the solution.. any one can help me to get these data from listbox..

regards.
sasi
Posted

Assuming you can read the values from the listbox, try splitting the string with an empty character. You will end up having an array of string values, from which you can choose the values you need.

string s = "  -rwxrwxrwx 1 owner   group   3233 Jul 27 18:28 1.log ";
string[] theSplit = s.Split(' ');
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 9:56am    
This is never needed! Please see my answer.
--SA
well....
Trim() the string!
Than create this:
String MyLine[] = listBox1.SelectedIndex.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

string fileName = MyLine[MyLine.Length-1];
string time = MyLine[MyLine.Length-2];
string day = MyLine[MyLine.Length-3];
string monthname = MyLine[MyLine.Length-4];
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 9:56am    
This is never needed! Please see my answer.
--SA
I assume You string(" -rwxrwxrwx 1 owner group 3233 Jul 27 18:28 1.log ") will be always contains 9 items(ignoring the whitespaces),

Following might help you,

C#
static void Main(string[] args)
{
    string data = "   -rwxrwxrwx 1 owner   group   3233 Jul 27 18:28 1.log";
    var result = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Skip(5);
    var date = string.Concat(result.FirstOrDefault(), result.Skip(1).FirstOrDefault());
    var time = result.Skip(2).FirstOrDefault();
    var fileName = result.Skip(3).FirstOrDefault();
}


:)
 
Share this answer
 
Comments
Herman<T>.Instance 1-Aug-11 8:53am    
uhoh...you ASSUME?
Mohammad A Rahman 1-Aug-11 9:05am    
Thanks :)
Sergey Alexandrovich Kryukov 1-Aug-11 9:56am    
This is never needed! Please see my answer.
--SA
You can do whatever you want with the strings stored in a list box, but you real mistake is that you're not working with the original data. You use some data to populate a list box, you do it in the form of strings; and then you try to extract the same data from the strings.

This is a completely wrong approach. There are two fixes.

First, you need to have some data layer. You use the data layer to populate UI and you use the data layers to do all other operations — with "real" original data, not string.

Second approach: you can and most usually should store actual "real" data in the list box, not string representation of it. The thing is: you can store instances of any type in the list view. The only problem is: what will be displayed in the list items? The answer is: whatever the method ToString returns. So, the solution is: create some data type, call it ListItemHelper. Put your original data in this type. Also, override object.ToString in this type to show your data the way you want. In this way, you always format data as string and never try to interpret this string as data.

—SA
 
Share this answer
 
v3

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