get char position in textBox - c#

I am trying to get the index of a each char in the text "ABCDCEF" (textBox.text). The problem is that the first 'C' index is 2 and the second C index is 4 but the second 'C' index in the result is 2 too.
This is the code:
foreach (char character in textBox1.Text)
{
MessageBox.Show(character + " - " + textBox1.Text.IndexOf(character));
}
Result:
char - index
A - 0
B - 1
C - 2
D - 3
C - 2
E - 5
F - 6
The correct result should be:
char - index
A - 0
B - 1
C - 2
D - 3
C - 4
E - 5
F - 6
Why it's happening?
Thanks

string.IndexOf returns first occurrence of a character, that's why it returns index 2 for c lookup.
MSDN Says,
Reports the zero-based index of the first occurrence of a specified
Unicode character or string within this instance. The method returns
-1 if the character or string is not found in this instance.
You could convert it to for loop and get index for each character.
for(int i=0;i<textBox1.Text.Length;i++)
{
MessageBox.Show(textBox1.Text[i] + " - " + i);
}

Related

How do I tell how far an item is in a string in a list in C#

Example:
List<string> letters = new List<string>();
letters.Add("A");
letters.Add("B");
letters.Add("C");
letters.Add("D");
letters.Add("E");
letters.Add("F");
letters.Add("G");
letters.Add("H");
letters.Add("I");
letters.Add("J");
letters.Add("K");
letters.Add("L");
letters.Add("M");
letters.Add("N");
letters.Add("O");
letters.Add("P");
letters.Add("Q");
letters.Add("R");
letters.Add("S");
letters.Add("T");
letters.Add("U");
letters.Add("V");
letters.Add("W");
letters.Add("X");
letters.Add("Y");
letters.Add("Z");
If I had somebody input a letter in the list, how could I tell which number of the alphabet it is
You can request the zero-based index of a listitem by using List<T>.IndexOf(item).
However, for finding out what letter of the alphabet a character is I suggest converting the char to an integer and substracting the offset: (Int32)[charToCheck]-96, e.g. (Int32)'a'-96 will return 1
You could write an extension method for this purpose like so:
// startingIndex specifies if the first character of the alphabet should be 0 or any other number
public static Int32 GetPositionInAlphabet(this char character, Int32 startingIndex = 0)
{
return (Int32)Char.ToLower(character) - 97 + startingIndex;
}
It is letters.IndexOf(theletter) + 1
IndexOf returns the 0-based index, that is, 0 for the first item, 1 for the second item, etc. This is why the + 1 is needed.

Regular expression to include starting string

I have managed to match into groups as follows using the below expression but its incomplete.
\([^\)]*\)
Example strings are,
s11(h 1 1 c)(h 1 1 c) x="" y="" z="" phi="" theta=""
e(45,10,h 1 1 c,1,cross,max) x="" y="" z="" phi="" theta=""
With the above expression I can match (h 1 1 c)(h 1 1 c) and (45,10,h 1 1 c,1,cross,max)
But I want to capture the starting string s11 and e along with (h 1 1 c)(h 1 1 c) and (45,10,h 1 1 c,1,cross,max)
You can use
var lines = new List<string> { "s11(h 1 1 c)(h 1 1 c) x=\"\" y=\"\" z=\"\" phi=\"\" theta=\"\"",
"e(45,10,h 1 1 c,1,cross,max) x=\"\" y=\"\" z=\"\" phi=\"\" theta=\"\""};
foreach (var s in lines)
{
Console.WriteLine("==== Next string: \"" + s + "\" =>");
Console.WriteLine(string.Join(", ",
Regex.Matches(s, #"\w+(?:\([^()]*\))+").Cast<Match>().Select(x => x.Value)));
Console.WriteLine("=== With groups and captures:");
var results = Regex.Matches(s, #"(\w+)(?:(\([^()]*\)))+");
foreach (Match m in results)
{
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(string.Join(", ", m.Groups[2].Captures.Cast<Capture>().Select(z => z.Value)));
}
}
See the C# demo. Output:
==== Next string: "s11(h 1 1 c)(h 1 1 c) x="" y="" z="" phi="" theta=""" =>
s11(h 1 1 c)(h 1 1 c)
=== With groups and captures:
s11
(h 1 1 c), (h 1 1 c)
==== Next string: "e(45,10,h 1 1 c,1,cross,max) x="" y="" z="" phi="" theta=""" =>
e(45,10,h 1 1 c,1,cross,max)
=== With groups and captures:
e
(45,10,h 1 1 c,1,cross,max)
Depending on what exact results you want to get, you may use a regex with or without capturing groups:
\w+(?:\([^()]*\))+
(\w+)(?:(\([^()]*\)))+
See the regex 1 demo and regex 2 demo.
Details
\w+ - one or more word chars (letters, digits and some connector puncutation)
(?:\([^()]*\))+ - one or more repetitions of
\( - a ( char
[^()]* - zero or more chars other than ( and )
\) - a ) char.

String validations c#

I have a 9 character string I am trying to provide multiple checks on. I want to first check if the first 1 - 7 characters are numbers and then say for example the first 3 characters are numbers how would I check the 5th character for a letter range of G through T.
I am using c# and have tried this so far...
string checkString = "123H56789";
Regex charactorSet = new Regex("[G-T]");
Match matchSetOne = charactorSetOne.Match(checkString, 3);
if (Char.IsNumber(checkString[0]) && Char.IsNumber(checkString[1]) && Char.IsNumber(checkString[2]))
{
if (matchSetOne.Success)
{
Console.WriteLine("3th char is a letter");
}
}
But am not sure if this is the best way to handle the validations.
UPDATE:
The digits can be 0 - 9, but can concatenate from one number to seven. Like this "12345T789" or "1R3456789" etc.
It'a easy with LINQ:
check if the first 1 - 7 characters are numbers :
var condition1 = input.Take(7).All(c => Char.IsDigit(c));
check the 5th character for a letter range of G through T
var condition2 = input.ElementAt(4) >= 'G' && input.ElementAt(4) <= 'T';
As it is, both conditions can't be true at the same time (if the first 7 chars are digits, then the 5th char can't be a letter).

How to format a number such that first 6 digits and last 4 digits are not hidden

How to format a number such that first 6 digits and last 4 digits are not hidden
such that 111111111111111 looks like 111111****1111
You can also use LINQ, substituting chars with indexes more than 5 and less than number.Length - 4 with *:
string number = "111111111111111";
string res = new string(number.Select((c, index) => { return (index <= 5 || index >= number.Length - 4) ? c : '*'; }).ToArray());
Console.WriteLine(res); // 111111*****1111
One simple way to do this is to slit the input..
int number = 111111111111111;
string firstsix = number.ToString().Substring(0,6) //gets the first 6 digits
string middlefour = number.ToString().Substring(6,4) //gets the next 4
string rest = number.ToString().Substring(10, number.ToString().Lenght) //gets the rest
string combined = firstsix + "****" +rest;
You need to use \G anchor in-order to do a continuous string match.
string result = Regex.Replace(str, #"(?:^(.{6})|\G).(?=.{4})", "$1*");
DEMO
IDEONE

Regarding finding middle element of a string

The below snippet takes a string as input. What I am trying is to get middle 2 elements of the string if the length is even.
string input = "confir";
string op = "";
op = input.Substring((input.Length - 1) / 2,input.Length/2 -1);//logic
Console.WriteLine(op);//display the output
Output for above snippet is nf.
When input is changed to confirme, output should be fi and not fir
How do I generalize? What is the error in the logic?
Second argument of String.Substring is a length of substring, not index like in Java. So if you need to get substring of two characters, pass 2:
string input = "confirme";
string op = input.Substring((input.Length - 1) / 2, 2);
BTW you should handle case when string is less than 2 characters long:
string op = input.Substring((input.Length - 1) / 2, Math.Min(input.Length, 2));
Tests:
input | op |
---------------------
"" | "" |
"c" | "c" |
"co" | "co" |
"con" | "on" |
"conf" | "on" |
"confir" | "nf" |
"confirme" | "fi" |
string input = "confir";
if(input.Length % 2 == 0)
Console.WriteLine(input.Substring((input.Length / 2)-1, 2));
That should give you the expected result.First check whether the string length is even then instead of (input.Length - 1) / 2, divide length by 2 and subtract 1,then take two characters like this: (input.Length / 2) - 1
Problem : you are providing total length of the string as second argument to the Substring() method.
Solution : Substring() method takes total number of characters tobe removed starting from first argument .
From MSDN : Substring(Int32, Int32)
Retrieves a substring from this instance. The substring starts at a
specified character position and has a specified length.
Replace This:
op = input.Substring((input.Length - 1) / 2,input.Length/2 -1);//logic
With This:
op = input.Substring((input.Length - 1) / 2,2);//logic
Suggestion : you need to check for Empty String and for Even number of charatcters in String.
Complete Code:
string input = "confirme";
string op = "";
if (input.Length > 0 && input.Length % 2 == 0)
{
op = input.Substring((input.Length - 1) / 2, 2);//logic
Console.WriteLine(op);//display the output
}

Categories

Resources