Using Substring to get digit before a string - c#

I am reading a file in C#. I want to check value from a string. The line consists as following:
15 EMP_L_NAME HAPPENS 5 TIMES.
40 SUP HAPPENS 12 TIMES.
I want to find the number of times which is in the string before the string "TIMES". I have written the following code:
int arrayLength = 0;
int timesindex = line.IndexOf("TIMES");
if (timesindex > 0)
{
//Positon of the digit "5" in the first line
int indexCount = timesindex - 2;
if (int.TryParse(line.Substring(indexCount, 1), out occursCount))
{
arrayLength = occursCount;
}
}
Using the above code, I can find the number of "TIMES" for a single digigt number. But if it is a double digit, it won't work( e.g the second line). I have to develop a logic to find the digit which is separted by a space with "TIMES". How I can do that?

You can do:
Split your string on space and remove empty enteries.
Find Index of "TIMES."
Access element Index - 1
Like:
string str = "15 EMP_L_NAME HAPPENS 5 TIMES. ";
string[] array = str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int index = Array.IndexOf(array, "TIMES.");
int number;
if (!int.TryParse(array[index - 1], out number))
{
//invalid number
}
Console.WriteLine(number);

If the input is reliable you can do a quicky with String.Split()...
int arrayLength = 0;
int timesindex = line.IndexOf("TIMES");
if (timesindex > 0)
{
string[] items = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
if (int.TryParse(items[items.Length - 2], out occursCount))
{
arrayLength = occursCount;
}
}
This method relies on the desired number being the second from last "word" in each line

If your strings are always the same format, with exactly five words or "sections" or whatever you want to call them, you could use:
int times = 0;
Int32.TryParse(line.Split(' ')[3], out times);
This would have to be more robust there's a chance the number may not exist in the string, or the string is in a completely different format.

Look at LastIndexOf combined with your timesindex. You can look for the space before the space before (timesindex-1), and then you have the two positions around the number.
int timesindex = line.IndexOf("TIMES");
int firstSpace = line.LastIndexOf(" ", timesindex-1);
string number = line.Substring(firstSpace, timesindex-firstSpace);
Though this might need some adjustments on the indexes, but that's the idea anyway

Try this
int timesindex = line.IndexOf("TIMES");
int happensindex = line.IndexOf("HAPPENS") + 7; //Add 7 cause HAPPEND is 7 chars long
if (timesindex > 0)
{
//Positon of the digit "5" in the first line
if (int.TryParse(line.Substring(happensindex, timesindex).trim(), out occursCount))
{
arrayLength = occursCount;
}
}

A Regex would be cleaner:
var regex = new Regex(#"(\d+)\sTIMES"); // match a number followed by whitespace then "TIMES"
string num = regex.Match(" 15 EMP_L_NAME HAPPENS 5 TIMES").Groups[1].ToString();
int val = int.Parse(num);

Using LINQ:
string[] lines = {"15 EMP_L_NAME HAPPENS 5,1 TIMES.", "40 SUP HAPPENS 12 TIMES. "};
var allValues = lines.Select(line =>
{
double temp;
var words = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var value = words[Array.IndexOf(words,"TIMES.") - 1];
if (double.TryParse(value, out temp)) return temp;
else return 0;
}).ToList();
foreach (var value in allValues)
{
Console.WriteLine(value);
}
// Output:
// 5,1
// 12

You can use System.Text.RegularExpressions.Regex, i.e. a regular expression, in order to find a pattern in a string:
string input = "40 SUP HAPPENS 12 TIMES.";
Match match = Regex.Match(input, #"(?<=HAPPENS\s)\d+(?=\sTIMES)");
if (match.Success) {
Console.WriteLine(match.Value); '==> "12"
}
Explanation of the regular expression: It uses the general pattern (?<=prefix)find(?=suffix) in order to find a position between a prefix and suffix.
(?<=HAPPENS\s) Prefix consisting of "HAPPENS" plus a whitespace (\s)
\d+ A digit (\d) repeated one or more times (+)
(?=\sTIMES) Suffix consisting of a whitespace (\s) plus "TIMES"
If you only want to test for "TIMES" but not for "HAPPENS", you can just drop the first part:
Match match = Regex.Match(input, #"\d+(?=\sTIMES)");
Since you are using the same search pattern many times, it is advisable to create a Regex once instead of calling a static method:
Regex regex = new Regex(#"\d+(?=\sTIMES)");
// Use many times with different inputs
Match match = regex.Match(input);

Related

Skipping a range of values in for loop C#

I'm trying to cycle through chars in a string.
string cycleMe = "Hi StackOverflow! Here is my string."
However, I want to skip over certain ranges of indexes. The ranges I want to skip over are stored in a List of objects, delims.
List<Delim> delims = delimCreator();
To retrieve each starting index and ending index for a range, I have to write a loop that accesses each "delim":
delims[0].getFirstIndex() //results in, say, index 2
delims[0].getLastIndex() //results in, say, index 4
delims[1].getFirstIndex() //results in, say, index 5
delims[1].getLastIndex() //results in, say, index 7
(there can be infinitely many "delim" objects in play)
If the above were my list, I'd want to print the string cycleMe, but skip all the chars between 2 and 4 (inclusive) and 5 and 7 (inclusive).
Expected output using the numbers above:
HiOverflow! Here is my string.
Here is the code I have written so far. It loops far more often than I'd expect (it loops ~x2 the number of characters in the string). Thanks in advance! =)
List<Delim> delims = delimAggregateInator(displayTextRaw);
for (int x = 0; x < cycleMe.Length;x++){
for (int i = 0; i < delims.Count; i++){
if (!(x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex())){
Debug.Log("test");
}
}
I assume that by skipping you meant you want to omit those characters from the original string. If that is the case, you can try Aggregate extension method like below.
string result = delims.Aggregate<Delim, string>(cycleMe, (str, d) => cycleMe = cycleMe.Remove(d.FirstIndex, (d.LastIndex - d.FirstIndex) + 1));
Make sure that the delim list is in the proper order.
Solution might be converting the string to char array, replacing the desired parts to spaces, and converting the output back to string.
Here is the modified version of your code:
string cycleMe = "Hi StackOverflow! Here is my string."
var charArray = cycleMe.ToCharArray(); // Converting to char array
List<Delim> delims = delimAggregateInator(displayTextRaw);
for (int x = 0; x < cycleMe.Length;x++){
for (int i = 0; i < delims.Count; i++){
// ORIGINAL: if (!(x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex())){
if (x >= delims[i].getFirstIndex() && x <= delims[i].getLastIndex()){
Debug.Log("test");
charArray[x] = ' '; // Replacing the item with space
}
}
string output = new string(charArray); // Converting back to string
P.S. This is probably not the most optimal solution but at least it should work.
You should use LINQ for that
struct Delim
{
public int First { get; set; }
public int Last { get; set; }
}
static void Main(string[] args)
{
string cycleMe = "Hi StackOverflow! Here is my string.";
var delimns = new List<Delim> { new Delim { First=2, Last=4}, new Delim { First = 5, Last = 7 } };
var cut = cycleMe.Where((c, i) =>
!delimns.Any(d => i >= d.First && i <= d.Last));
Console.WriteLine(new string(cut.ToArray());
}
That means I am basically only selecting letters, at positions which are not part of any cutting range.
Also: Fix your naming. A delimiter is a character, not a position (numeric)

Import x and y coordinates from .txt file

I need to read coordinates from text file for my project,but the text file is like this
1 37.4393516691 541.2090699418
2 612.1759508571 494.3166877396
3 38.1312338227 353.1484581781
and with more space in front.I have code that i tried but i couldn't make the seperators work.
1 1150.0 1760.0
2 630.0 1660.0
3 40.0 2090.0
The code:
string[] cityPositions = File.ReadAllLines(ofd.FileName);
foreach (string cityP in cityPositions)
{
int startIndexX = cityP.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase) + 3;
int endIndexX = cityP.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase);
int X = int.Parse(cityP.Substring(startIndexX, endIndexX - startIndexX));
int startIndexY = cityP.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase) + 3;
int endIndexY = cityP.IndexOf("", StringComparison.CurrentCultureIgnoreCase);
int Y = int.Parse(cityP.Substring(startIndexY, endIndexY - startIndexY));
create_City(new Point(X, Y));
}
If you want to make use of Regular Expressions, try this:
String expression = #"(\d)([\d\.]*)([\d\.]*)";
Regex r = new Regex(expression);
foreach (String cityPosition in cityPositions)
{
MatchCollection mc = r.Matches(cityPosition);
int index = Convert.ToInt32(mc[0].Value);
decimal coOrdX = Convert.ToDecimal(mc[1].Value);
decimal coOrdY = Convert.ToDecimal(mc[2].Value);
Console.WriteLine(String.Format("{0} {1} {2}", index, coOrdX, coOrdY));
}
This code will produce this output:
1 1150.0 1760.0
2 630.0 1660.0
3 40.0 2090.0
It should disregard the varying amount of space between the values and make it easier to parse the three different values in the file.
First of all your data type doesnt match. You should use double for coordinates, and use Split method, for example:
double X = double.Parse(cityP.Split()[1], CultureInfo.InvariantCulture);
double Y = double.Parse(cityP.Split()[2], CultureInfo.InvariantCulture);
Split function with no parameters given will split by whitespace.
EDIT
It's not clear in the question but, if your lines dont have the same pattern, and have different spaces use Split method like this:
double X = double.Parse(cityP.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);
Instead of working with indices, you could Replace multiple whitespaces with a single whitespace, like
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
cityP = regex.Replace(cityP, " ");
then use split to get the coordinates
var x = double.Parse(cityP.Split()[1]);
var y = double.Parse(cityP.Split()[2]);

Count the number of contiguous equal characters

I need to validate Serial numbers and one of the rules is that there are up to 5 contiguous equal characters allowed.
Example valid:
012W212222123 // 4x the digit 2 contiguous
Example invalid:
012W764444443 // 6x the digit 4
So I tried to get the maximum number of contiguous characters without success
int maxCount = "012W764444443".GroupBy(x => x).Max().Count();
I suggest using a regex for a check to see if there are 5 or more consecutive digits:
Regex.IsMatch(input, #"^(?!.*([0-9])\1{4})")
If any characters are meant:
Regex.IsMatch(input, #"^(?!.*(.)\1{4})")
See the regex demo
The regex finds a match in a string that contains less than 5 identical consecutive digits (version with [0-9]) or any characters other than a newline (version with .).
Details:
^ - start of string
-(?!.*(.)\1{4}) - a negative lookahead that fails the match if the pattern is matched:
.* - any 0+ chars other than a newline
(.) - Group 1 capturing any char but a newline
\1{4} - exactly 4 consecutive occurrences of the same value stored inside Group 1 (where \1 is a backreference and the {4} is a range/bound/limiting quantifier).
C#:
var strs = new List<string> { "012W212222123", "012W764444443"};
foreach (var s in strs)
Console.WriteLine("{0}: {1}", s, Regex.IsMatch(s, #"^(?!.*(.)\1{4})"));
Yet another option is to use this function:
public static int MaxNumberOfConsecutiveCharacters(string s)
{
if (s == null) throw new ArgumentNullException(nameof(s));
if (s.Length == 0) return 0;
int maxCount = 1;
int count = 1;
for (int i = 1; i < s.Length; i++)
{
if (s[i] == s[i-1])
{
count++;
if (count > maxCount) maxCount = count;
}
else
{
count = 1;
}
}
return maxCount;
}
Obviously, this is a lot more code than a regular expression. Depending on your knowledge of regular expressions, this may or may not be more readable to you. Also, this is probably more efficient than using a regular expression, which may or may not be important to you.
It's a little inefficient, but this works:
var max =
"012W212222123"
.Aggregate(
new { Char = ' ', Count = 0, Max = 0 },
(a, c) =>
a.Char == c
? new { Char = c, Count = a.Count + 1, Max = a.Max > a.Count + 1 ? a.Max : a.Count + 1 }
: new { Char = c, Count = 1, Max = a.Max > 1 ? a.Max : 1 })
.Max;
I tried with both inputs and got the right number of maximum repeats each time.

How many string2 exist in string1?

I'm trying to find an algorithm that takes an input from user (string1) and then takes another (string2) then counts How many string2 exist in string1.
for example:
there is 2 # in string1 here
string1="##"
string2="#"
answer:2
there is 2 ## in string1:(1st one with the index of 0,1 and 2nd one with the index of 1,2)
string1="###"
string2="##"
answer=2
there is 3 ## in string1:(1st:index of 0,1 2nd:index of 1,2 3rd:index of 2,3)
string1="####"
string2="##"
answer=3
for counting # here we can simply do this:
string1.Length - string2.Length + 1
but I can only make the algorithm work for examples like these, while inputs can be anything. now i need an algorithm to do this for me.
Here is ,y code that works for my inputs, but doesn't work for all inputs.
for example if string2 was #a.
string string1 = "#abc##asd###12####";
string string2 = "##";
char[] str2 = string2.ToCharArray();
string[] str1 = string1.Split(str2);
string chars = "";
foreach (string s in str1)
foreach (char c in s)
chars += c;
//Result for chars are:a, b, c, a, s, d, 1, 2
string[] splits = string1.Split(chars.ToCharArray());
//Result for splits are:#, ##, ###, ####
int sum = 0;
for (int i = 0; i < splits.Length; i++)
{
if (splits[i].Length<string2.Length)
continue;
else
sum += splits[i].Length - string2.Length + 1;
}
Console.WriteLine(sum);
In my algorithm i seprated string1 by string2 characters and then count strings[] that was created by split method.
I already know that my algorithm is completely wrong, but I couldn't solve it anyway. I just put the code to show what i have tried.
if you write algorithm in your answer, that would be nice. I also have a little knowledge about lambda and linq so no problem if you could to this by lambda or linq or func or expression but explain about it.
Here's a working code :
string string1 = "#abc##asd###12####";
string string2 = "##";
List<int> indexes = new List<int>();
int index = -1;
while((index = string1.IndexOf(string2, index + 1)) >= 0)
{
indexes.Add(index);
}
Console.WriteLine(indexes.Count);

Split a long string into an array of shorter strings

How can I split a string of around 300 (n) words into an array of n/30 strings of 30 words?
You can use Regex.Matches:
string[] bits = Regex.Matches(input, #"\w+(?:\W+\w+){0,29}")
.Cast<Match>()
.Select(match => match.Value)
.ToArray();
See it working online: ideone
A Regex split would make sense if you have a very large or a very small of characters that can be a part of your string. Alternatively, you can use the Substring method of the String class to get the desired result:
string input = "abcdefghijklmnopqrstuvwxyz";
const int INTERVAL = 5;
List<string> lst = new List<string>();
int i = 0;
while (i < input.Length)
{
string sub = input.Substring(i, i + INTERVAL < input.Length ? INTERVAL : input.Length - i);
Console.WriteLine(sub);
lst.Add(sub);
i += INTERVAL;
}

Categories