Delimiters - Is there a simple way
-
You may want to try regular expressions...
"too much daily WTF for someone..." - Anton Afanasyev
-
-
haz13 wrote:
what do you mean by regular expressions?
Here: http://en.wikipedia.org/wiki/Regular_expression[^] http://www.regular-expressions.info/[^]
#region signature my articles #endregion
-
-
haz13 wrote:
what do you mean by regular expressions?
Here: http://en.wikipedia.org/wiki/Regular_expression[^] http://www.regular-expressions.info/[^]
#region signature my articles #endregion
Giorgi Dalakishvili wrote:
That looks like a bookmark worthy site to me :-D
"too much daily WTF for someone..." - Anton Afanasyev
-
I have a text file of the following format "apple","orange","pear" etc I have tried both char and string seperators but can't think of a way grab the data cleanly without including a " or , . I either end up with "apple" "orange" "pear" or "apple orange pear" The code I have been using.
private static char[] charSeperator = new char[] { ',' };
private static string[] strSeperator = new string[] {"\",\""};
strArray = strLine.Split(charSeperator);
strArray = strLine.Split(strSeperator,StringSplitOptions.None);
Is there a simple way of grabing apple orange and pear from the text file, without having to do further data cleaning? Thanks in advance for any help you can offerHaz
using System.Text.RegularExpressions; // the namespace for using Substring strArray = strLine.Split(new char[] { ',' }; // Use this still, but with the modifications below foreach (string fruit in strArray) // a loop that runs for every piece of fruit in strArray { int fruitlength = fruit.Length; // getting the length of the fruit string fruitlength = fruitlength - 2; // subtracting 2 because of quotes string trimmedfruit = fruit; trimmedfruit = fruit.Substring(1,fruitlength); // basically the method Substring(start,length) // and the first letter starts at 0 and length is how many you want to cut out // so I took the total length of string "apple", subtracted 2 because of quotes // then started the trim at 1 just after the first qoute and pulled out all the letters // minus the qoutes }
-
using System.Text.RegularExpressions; // the namespace for using Substring strArray = strLine.Split(new char[] { ',' }; // Use this still, but with the modifications below foreach (string fruit in strArray) // a loop that runs for every piece of fruit in strArray { int fruitlength = fruit.Length; // getting the length of the fruit string fruitlength = fruitlength - 2; // subtracting 2 because of quotes string trimmedfruit = fruit; trimmedfruit = fruit.Substring(1,fruitlength); // basically the method Substring(start,length) // and the first letter starts at 0 and length is how many you want to cut out // so I took the total length of string "apple", subtracted 2 because of quotes // then started the trim at 1 just after the first qoute and pulled out all the letters // minus the qoutes }
jblouir wrote:
using System.Text.RegularExpressions; // the namespace for using Substring
Sorry, but, what are you thinking ? Substring is a method on a class, you don't need a namespace to call it. string trimmedFruit = fruit.Replace("\"", ""); is even easier.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
jblouir wrote:
using System.Text.RegularExpressions; // the namespace for using Substring
Sorry, but, what are you thinking ? Substring is a method on a class, you don't need a namespace to call it. string trimmedFruit = fruit.Replace("\"", ""); is even easier.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Your right, didn't realise. Still learning, on week 1 and a half now. At the point when I was learning how to use regular expressions I must have stumbled on to substring and assumed it was part of the namespace. and I didn't know replace even existed
-
I have a text file of the following format "apple","orange","pear" etc I have tried both char and string seperators but can't think of a way grab the data cleanly without including a " or , . I either end up with "apple" "orange" "pear" or "apple orange pear" The code I have been using.
private static char[] charSeperator = new char[] { ',' };
private static string[] strSeperator = new string[] {"\",\""};
strArray = strLine.Split(charSeperator);
strArray = strLine.Split(strSeperator,StringSplitOptions.None);
Is there a simple way of grabing apple orange and pear from the text file, without having to do further data cleaning? Thanks in advance for any help you can offerHaz
haz13 wrote:
I have tried both char and string separators but can't think of a way grab the data cleanly without including a " or , . I either end up with "apple" "orange" "pear" or "apple orange pear"
I used similar code to split it. Heres some code, maybe it can help u. Input text same as u : "apple","orange","pear" etc assume the string named "str"
string[] strArray = str.split(',');
for (int i=0;iTraining makes perfect....