Reputation: 91
In C# how do I determine if a char at a certain index is between two characters in a string. I'm trying to do this to remove all spaces between quotes in a string. Example syntax: isBetween(string str, int index, char start, char end)
Thanks in advance
Edit: I also need it to work if start and end are the same character
Edit2: To clarify, I need it to work not only directly between, but it needs to work for other strings like isBetween("as((sup)hello)as", 5, '(', ')')
Upvotes: 1
Views: 1545
Reputation: 889
try this
public static bool IsBetween(String str, int index, char start, char end)
{
var startIndex = str.Substring(0, index).LastIndexOf(start);
var LastIndex = str.Substring(index).IndexOf(end);
if (startIndex == -1 || LastIndex == -1)
return false;
LastIndex = LastIndex + index;
return startIndex <= (index - 1) && LastIndex >= (index - 1);
}
Upvotes: 0
Reputation: 1515
You can do this using RegularExpressions Demo
public class Program {
public static void Main()
{
String xs = "aBc";
var x= xs.ReplaceInBetween('a', 'c', 'B', 'b'); }
}
public static class Extensions {
public static string ReplaceInBetween(this String str, char start, char end, char middle, char replacewith)
{
Regex x = new Regex($"([{start}])({middle})([{end}])");
str= x.Replace(str, "$1" + replacewith + "$3");
return str;
}
}
Upvotes: 1
Reputation: 186813
Well, fro a public methid we should validate str
, index
and then check for chars at index - 1
, index + 1
:
public static bool IsBetween(this String str, int index, char start, char end) {
return str != null && // not null
index >= 1 && index < str.Length - 1 && // valid index
str[index - 1] == start && // char at index is between
str[index + 1] == stop;
}
However, if you want to remove some characters (say, enquoted spaces), I suggest building a new string, e.g.
// "abc " 123 456 789" pq" -> "abc "123456789" pq"
public static string RemoveQuotedSpaces(String str) {
if (string.IsNullOrEmpty(str))
return str;
StringBuilder sb = new StringBuilder(str.Length);
bool inQuotation = false;
foreach (char c in str) {
if (c == '"')
inQuotation != inQuotation;
if (!inQuotation || c != ' ')
sb.Append(c);
}
return sb.ToString();
}
Upvotes: 0
Reputation: 1515
what I understood form your question. you are looking for something like this.
public class Program {
public static void Main()
{
var isBetween = "abc".IsBetween('b', 'a', 'c', out int i);
Console.WriteLine(isBetween); //True
Console.WriteLine(i); //True
}
}
public static class Extensions {
public static bool IsBetween(this String str, char middle, char start, char end, out int index)
{
index = - 1;
var left = str.IndexOf(start);
var right = str.IndexOf(end);
index = str.IndexOf(start) + 1 == str.IndexOf(end) -1 ? str.IndexOf(end) - 1: -1 ;
return str[index] == middle ;
}
}
@ThePerplexedOne I did reuse your code.
Upvotes: 1
Reputation: 2950
Based on the information given, and from what I understand about your question, you want an extension method for strings.
Something like this:
public class Program
{
public static void Main()
{
var isBetween = "abc".IsBetween(1, 'a', 'c');
Console.WriteLine(isBetween); //True
}
}
public static class Extensions
{
public static bool IsBetween(this String str, int index, char start, char end)
{
var left = str[index - 1];
var right = str[index + 1];
return left == start && right == end;
}
}
The code above will check if the character at index 1 (which is b
), is between two characters (a
and c
). This returns true.
(Note that this does not account for index out of bound exceptions)
Upvotes: 2