Reputation: 507
I have a string like this ..
2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0
Now as per my requirement i have to split the substrings like 2931
,06/03/13
and O006129009857
.I have used IndexOf
method of string to get the start and end index of substring as soon as a empty space comes like this in the code..
int startIndex = line.IndexOf(Calldate);
int endIndex = line.IndexOf(" ", startIndex);
int difference = endIndex - startIndex;
strSubstring = (startIndex + "," + difference);
As we can see to endIndex i have taken line.IndexOf(" ", startIndex);
while now i want to take next character for Example if i have to take O006129009857
then i have to count till **
or any other character available at that place to calculate the endIndex
..
Please help me to calculate the endIndex
till next character comes instead of .
Please help me .Thanks in advance..
Upvotes: 0
Views: 500
Reputation: 8355
It's a little unclear what you are asking but if I understand correctly, you want to calculate the number between a specific index in a string and the next character that is not a space. The following code assumes that you want to know the number of characters between foo and bar but that you know the start index.
string input = "foo bar";
int startIndex = 3;
string pattern = @"\S";
var regex = new Regex(pattern);
int endIndex = regex.Match(input, startIndex).Index;
int characterCount = endIndex - startIndex;
Upvotes: 0
Reputation: 860
cant you just use string.Split(' '); to return an array of string that are seperated by a space [" "]?
string CallDate = "2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0 ";
string[] parts = CallDate.Split(' ');
parts = parts.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
Your parts array looks like
{"2931", "8795", "012", "00:24:14", "06/03/13", "10:03", "O006129009857", "**", "0", "0"}
EDIT: To count the spaces between words, remove the line
parts = parts.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
and your array looks like
"2931", "8795", "", "012", "00:24:14", "06/03/13", "10:03", "O006129009857", "", "", "", "", "", "", "**", "", "", "", "", "0", "", "", "", "", "", "", "", "", "", "", "0", "", "", "", ""
The number of empty spaces between each word is the number of empty strings between each non-empty string. a simple for loop over the array will allow you to count the spaces (and therefore the position of each work in the string.
Upvotes: 0
Reputation: 188
The string.Split function will accomplish this for you:
string text = "2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0";
char[] Delimiters = new char[]{' '};
var result = text.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
The result will be 2931 8795 012 00:24:14 06/03/13 10:03 O006129009857 ** 0 0
Upvotes: 0
Reputation: 67968
^(\d+)|(\d+\/\d+\/\d+)|\b(\w\d+)\b(?=\s*\*\*)
Try this.just grab the captures.see demo.
http://regex101.com/r/qC9cH4/14
Upvotes: 1