Click here to Skip to main content
16,010,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
im having listview items as
COLUMN1
   IN 123
  OU 234
when i double click on the values, another form should be opened and it was opening. by the following code,

C#
string item1,item2;
item1 = listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text;
if (item1.StartsWith("IN"))
{
     Stock_Card_Drill_Form scds = new Stock_Card_Drill_Form("Stock_IN", "", "");
     scds.Show();
}
if (item1.StartsWith("OU"))
 {
     Stock_Card_Drill_Form scdo = new Stock_Card_Drill_Form("Stock_Out", "", "");
     scdo.Show();
}



in this code item1 is getting "IN 123" value if we double click on "IN 123" value... and in if condition if item1 starts with IN the specific form will be opened but what about the " 123" ??
i have to split the text"IN 123" into "IN", " " & "123" why three because, the text contain some space, i want to eliminate that space and text IN and 123 should be split into two values so that second value 123(values may differ) should be passed into item2 string which i specied in
C#
string item1, item2
so that i can use the split value for another cause..

so please help me to split the text..
Posted

You should simple use the Split function like in the next example:
C#
string[] result = item1.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
if(result.Length == 2)
{
  item1 = result[0];
  item2 = result[1];
  // Put here the code for using the data
  //...
}
else
{
 // Manage the error....
}
 
Share this answer
 
Hello


Try this

C#
string sValue =  listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text;

string[] arrValue = sValue.Split(' ');
string sBeforeSpace = arrValue[0];
string sAfterSpace = arrValue[1];
 
Share this answer
 
v3
Comments
chaitanya556 28-Oct-14 2:07am    
Thank you very much....
If you want to split the string with space, you can use
string[] splitItems = item1.Split(' ');


and you can use
Stock_Card_Drill_Form scds = new Stock_Card_Drill_Form("Stock_IN", splitItems[0] , splitItems[1] );


Note: It is better to check the length of the array before using it, so that you will not get a index out of bound of exception
 
Share this answer
 
v2
Hi,

You can also try it like this:

C#
string stringIN = "IN 123";
string stringOUT = "OU 123";

string[] split = stringIN.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

string[] split2 = stringOUT.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


Hope this helps !! :)

Regards,
Praneet
 
Share this answer
 
Simplest one would be
C#
string Digits = Regex.Match("IN 123", @"\d+").Value;//output : 123
string Characters = Regex.Replace("IN 123", @"[0-9 ]+", string.Empty);//output : IN

Regards..
 
Share this answer
 
v2

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