Splitting a string into characters, but keeping some together [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have this string: TF'E'
I want to split it to characters, but the '" character should join the character before it.
So it would look like this: T, F' and E'

You could use a regular expression to split the string at each position immediately before a new letter and an optional ':
var input = "TF'E'";
var output = Regex.Split(input, #"(?<!^)(?=\p{L}'?)");
output will now be a string array like ["T", "F'", "E'"]. The lookbehind (?<!^) ensure we never split at the start of the string, whereas the lookahead (?=\p{L}'?) describes one letter \p{L} followed by 0 or 1 '.

You can use a regex to capture "an uppercase character followed optionally by an apostrophe"
var mc = Regex.Matches(input, "(?<x>[A-Z]'?)");
foreach(Match m in mc)
Console.WriteLine(m.Groups["x"].Value);

If you don't like regex, you can use this method:
public static IEnumerable<string> Split(string input)
{
for(int i = 0; i < input.Length; i++)
{
if(i != (input.Length - 1) && input[i+1] == '\'')
{
yield return input[i].ToString() + input[i+1].ToString();
i++;
}
else
{
yield return input[i].ToString();
}
}
}
We loop through the input string. We check if there is a next character and if it is a '. If true, return the current character and the next character and increase the index by one. If false, just return the current character.
Online demo: https://dotnetfiddle.net/sPCftB

Related

string.IndexOf ignoring escape sequences [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to extract the CN of an LDAP DN string.
Here's an example string that illustrates the problem
var dn = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
What I want is the position of the first non escaped ',' character, which is at position 32.
var pos = dn.IndexOf(',');
returns the first comma, regardless of escaping or not. Now can I bring IndexOf to skip the escaped comma in the string?
Assuming that \ should be escaped by itself: \\ to put just \ you can implement a simple
finite state machine
private static int IndexOfUnescaped(string source,
char toFind,
char escapement = '\\') {
if (string.IsNullOrEmpty(source))
return -1;
for (int i = 0; i < source.Length; ++i)
if (source[i] == escapement)
i += 1; // <- skip the next (escaped) character
else if (source[i] == toFind)
return i;
return -1;
}
...
var dn = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
var pos = IndexOfUnescaped(dn, ',');
You can use Regex:
string s = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
Regex regex = new Regex("(?<!\\\\),", RegexOptions.Compiled);
int firstMatch = regex.Matches(s).FirstOrDefault()?.Index ?? -1;
Demo: https://regex101.com/r/Jxco8K/1
It's using a negative lookbehind, so check all commas and look if it's not preceeded by a backslash.
Colleague of mine whipped up this regex. Not entirely the question, but since I wanted the position to then use SubString it also does the trick.
var CnRegex = new Regex(#"([a-zA-Z_]*)=((?:[^\\,}]|\\.)*)");
var match = CnRegex.Match(input);
if (match.Success)
return match.Value;
return null;
I feared it would come down to a Regex, as in Tim's solution, or 'brute force' as with Dmitry's solution.

How to validate a 5 characters input of textbox that the first 3 characters to letters only and the last 2 characters to numbers c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to create a method that checks a textbox 5 character inputs. The first 3 characters should be letters and the last 2 characters should be numbers.
here's my current code:
public void checkInput(String s) {
if (CheckInputString(s)) {
//To Check if the first 3 characters are letters and check last 2 characters if numbers
}
else {
//Invalid
}
please help.
You can use RegEx
[A-Za-z]{3} - Matches 3 alpha
[0-9]{2} - Matches 2 numbers
Test your inputs with given regex online https://regex101.com/r/fF4zG9/5
Regex temp = new Regex("^[A-Za-z]{3}[0-9]{2}$");
string yourVal = "asd12";
if(temp.IsMatch(yourVal))
{
//Matches
}
else
{
//Fails
}
You can use a regular expression to check the string.
^[a-zA-Z]{3}[0-9]{2}$
Your code could look like this:
public bool CheckInputString(string s)
{
System.Text.Regex regex = new System.Text.Regex("^[a-zA-Z]{3}[0-9]{2}$");
return regex.IsMatch(s);
}
One way to do it is to simply validate each requirement on the string and then return true if they all pass.
public static bool IsValid(string input)
{
return input != null && // Not null
input.Length == 5 && // Is 5 characters
input.Take(3).All(char.IsLetter) && // First three are letters
input.Skip(3).All(char.IsDigit); // The rest are numbers
}

Match the number of commas in CSV line that are not between quotation marks with C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a CSV line and I need to count the number of columns in the line.
Some of the column values contains comma (in this case the value will be surround with quotation marks)
I need a Regex that will match only commas that are not surrounded with quotation marks.
For example:
a,b,c
will match 2 commas
and The line:
a,"b,c",d,"e,f"
will match 3 commas
Thanks,
Nadav.
I doubt if a complex regular expression will be better than an easy loop:
private static int CountCommas(String source, Char separator = ',') {
int result = 0;
Boolean inQuotation = false;
foreach (Char c in source)
if (c == '"')
inQuotation = !inQuotation;
else if ((c == separator) && !inQuotation)
result += 1;
return result;
}
Test
// 3
Console.Write(CountCommas("a, \"b,c\", d, \"e,f\""));

For loop check if string only has 3 capital letters followed by 4 numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I input a string that has to start with three capital letters and ending with four digits (like so: "SJL1036") the program is just supposed to check if my input follows that model.
if i were to input "Sjl1036" og "SJL103" it would output that it is a false statement.
Try this regular expression. 3 uppercase, 4 numbers.
^[A-Z]{3}[0-9]{4}$
For example:
var value = "FSK2526";
if (Regex.IsMatch(value, #"^[A-Z]{3}[0-9]{4}$")) {
// it matches
}
Although you could do it with for loop, but you could simplify it further with regex like:
Regex regex = new Regex(#"^[A-Z]{3}.*[0-9]{4}$");
Match match = regex.Match("SJL1036");
if (match.Success)
{
Console.WriteLine(match.Value);
}
If this is the requirement:
A string that has to start with three capital letters and ending with
four digits
Probably the most efficient approach is using string methods:
bool valid = input.Length >= 7
&& input.Remove(3).All(Char.IsUpper) // or input.Substring(0, 3)
&& input.Substring(input.Length - 4).All(Char.IsDigit);
If the actual requirement is "3 capital letters followed by 4 numbers"(so 7 characters) you just need to change input.Length >= 7 to input.Length == 7.
A non-Regex option, You can use a bit of LINQ like:
string str = "SJL1036";
if (str.Length == 7 &&
str.Take(3).All(char.IsUpper)
&& str.Skip(3).All(char.IsDigit))
{
Console.WriteLine("valid");
}
else
{
Console.WriteLine("invalid");
}

How to find the capital substring of a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find the capitalized portion of a string, to then insert two characters that represent the Double Capital sign in the Braille language. My intention for doing this is to design a translator that can translate from regular text to Braille.
I'll give an example belo.
English String: My variable is of type IEnumerable.
Braille: ,My variable is of type ,,IE-numberable.
I also want the dash in IE-numerable to only break words that have upper and lower case, but not in front of punctuation marks, white spaces, numbers or other symbols.
Thanks a lot in advance for your answers.
I had never heard of a "Double Capital" sign, so I read up on it here. From what I can tell, this should suit your needs.
You can use this to find any sequence of two or more uppercase (majuscule) Latin letters or hyphens in your string:
var matches = Regex.Matches(input, "[A-Z-]{2,}");
You can use this to insert the double-capital sign:
var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0");
For example:
var input = "this is a TEST";
var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0"); // this is a ,,TEST
You can use this to hand single and double capitals:
var input = "McGRAW-HILL";
var result = Regex.Replace(input, "[A-Z-]([A-Z-]+)?",
m => (m.Groups[1].Success ? ",," : ",") + m.Value); // ,Mc,,GRAW-HILL
You can find them with a simple regex:
using System.Text.RegularExpressions;
// ..snip..
Regex r = new Regex("[A-Z]"); // This will capture only upper case characters
Match m = r.Match(input, 0);
The variable m of type System.Text.RegularExpressions.Match will contain a collection of captures. If only the first match matters, you can check its Index property directly.
Now you can insert the characters you want in that position, using String.Insert:
input = input.Insert(m.Index, doubleCapitalSign);
this code can solve your problema
string x = "abcdEFghijkl";
string capitalized = string.Empty;
for (int i = 0; i < x.Length; i++)
{
if (x[i].ToString() == x[i].ToString().ToUpper())
capitalized += x[i];
}
Have you tried using the method Char.IsUpper method
http://msdn.microsoft.com/en-us/library/9s91f3by.aspx
This is another similar question that uses that method to solve a similar problem
Get the Index of Upper Case letter from a String
If you just want to find the first index of an uppercase letter:
var firstUpperCharIndex = text // <-- a string
.Select((chr, index) => new { chr, index })
.FirstOrDefault(x => Char.IsUpper(x.chr));
if (firstUpperCharIndex != null)
{
text = text.Insert(firstUpperCharIndex.index, ",,");
}
Not sure if this is what you are going for?
var inputString = string.Empty; //Your input string here
var output = new StringBuilder();
foreach (var c in inputString.ToCharArray())
{
if (char.IsUpper(c))
{
output.AppendFormat("_{0}_", c);
}
else
{
output.Append(c);
}
}
This will loop through each character in the inputString if the characater is upper it inserts a _ before and after (replace that with your desired braille characters) otherwise appends the character to the output.

Categories