This question already has answers here:
How do I get an array of repeated characters from a string using LINQ?
(3 answers)
Closed 7 years ago.
I take a string with integer count
int count=0;
string s="wow"
and i am using foreach loop to count number of characters in a specified string
foreach(char ch in s)
{
count++
}
so how can i count those characters which are repeated in my string like 'w'.
Try this )
string test = "aababc";
var result = test.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => new { charName = c.Key, charCount = c.Count()});
Do like this
string s="wow";
int repeats= s.Length - s.ToCharArray().Distinct().Count();
Related
This question already has answers here:
Splitting a string into chunks of a certain size
(39 answers)
Closed 3 years ago.
What's the most efficient way to split string values into an array every specified number of times? For example, split by 2:
string test = "12345678";
To:
string[] test = new[] {"12", "34", "56"};
What I've tried:
double part = 2;
int k = 0;
var test = bin.ToLookup(c => Math.Floor(k++ / part)).Select(e => new string(e.ToArray()));
You can use LINQ : when length was the length of part
var str = "12345678";
var length = 2;
var result = Enumerable.Range(0, (str.Length + length - 1) / length)
.Select(i => str.Substring(i * length, Math.Min(str.Length - i *length,
length)));
This question already has answers here:
Extract number at end of string in C#
(9 answers)
Regex for number at the end of string?
(5 answers)
Closed 3 years ago.
I need to check if the last character of a string ends with an int. I would like to avoid using a regular expression, because I think it is very hard, but if there is no smoother way of doing it, I will try it.
I basically tried something that looks like this:
text.EndsWith(INTEGER)]
Boolean result = char.IsDigit(text[text.Length - 1]);
using linq Last method and Isdigit function
bool isDigit = Char.IsDigit(("one1").Last());
Isolate the last character and try parse it to integer:
private static bool IsStringEndsWithDigit(string str)
{
if (String.IsNullOrEmpty(str))
{
return false;
// or throw an exception
// throw new Exception("string can not be empty");
}
string last = str.Substring(str.Length - 1, 1);
int num;
var isNum = int.TryParse(last, out num);
return isNum;
}
var lastChar = text.Last();
var endsWithNum = lastChar >= '0' && lastChar <= '9';
string s = "hello world0";
int len = s.Length;
List<char> digits = new List<char>(){'0','1','2','3','4','5','6','7','8','9'};
if (digits.Contains(s[len - 1]))
{
Console.WriteLine("last character is digits");
}
This question already has answers here:
Convert int (number) to string with leading zeros? (4 digits) [duplicate]
(4 answers)
Closed 5 years ago.
Hello i am trying to process character arrays.
I want to assign the number 50 as string value "00050". How can I do it ?
enter code here
string strRpc(int NumstrRpcSendLen)
{
int digit = Convert.ToInt32( Math.Floor(Math.Log10(NumstrRpcSendLen + 5) + 1));
int len = 0;
char[] d = new char[5];
string result= null;
while (len<5)
{
if (len<digit)
{
d[len] = '0';
}
else
{
}
len++;
}
return result;
}
I'm not sure what your code is doing, but there's a string format specifier for that:
var x = 50.ToString("D5");
//x = "000050"
Look at the documentation for more help
You could try it
var result = 50.ToString("00000");
This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 8 years ago.
I have 3 different string values in my console application:
Student A (AA.1)
Student(1) B (AA.2)
Student B
Would like to get value from backet:
AA.1, AA.2
Thanks. I have used split, however will encounter error because of Student B does not have any bracket and Student(1) B (AA.2) have 2 brackets.
Current Code
char[] targetExpression = new char[] { '(', ')' };
string title = "Student A (AA.1)";
Console.WriteLine(title.Split(targetExpression)[1]);
looking for something like this?
/.*\(([^)]*)\)$/
A function to get a string within characters
string GetStirngWithiin(string SourceString, char startChar, char endChar,int StartFrom=0)
{
SourceString = SourceString.Substring(StartFrom);
int startFrom=SourceString.IndexOf(startChar);
int EndAt=SourceString.IndexOf(endChar);
if ((startFrom > -1) && (EndAt >-1))
{
int SubStrLength = EndAt - startFrom - 1;
if (SubStrLength > 0)
{
string Result = SourceString.Substring(startFrom + 1, SubStrLength);
return Result;
}
}
return "";
}
you can use the function calls as
string str1 = GetStirngWithiin("Student A (AA.1)",'(',')');
string str2 = GetStirngWithiin("Student(1) B (AA.2))", '(', ')',10);
using with Student B will return a blank string.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse string to int array
I have string which comes from a numeric textbox like this: 2670053157. how should I split each character of string and insert them into List<int> elements?
var list = numberString.Select(c => Int32.Parse(c.ToString())).ToList();
Or, if you'd rather add to an existing list:
list.AddRange(numberString.Select(c => Int32.Parse(c.ToString()));
var list = new List<int>();
list.AddRange(
from character in numericString
select int.Parse(character));
List<int> numericlist = "2670053157".Select(c => c - '0').ToList();
If you're afraid of exceptions being thrown due to improper inputs, you could always go the safe route:
// string input = TextBox1.Text;
List<int> intList = new List<int>();
foreach (char c in input)
{
int i;
if (Int32.TryParse(c.ToString(), out i))
{
intList.Add(i);
}
}
Start out with a helper method:
public static IEnumerable<short> getDigits(long input)
{
while (input > 0)
{
yield return (short)(input % 10);
input /= 10;
}
}
Then if you want the values in a list, just call ToList:
List<short> list = getDigits(2670053157).ToList();
If you want the higher order bits first you'll need to Reverse the sequence:
List<short> list = getDigits(2670053157).Reverse().ToList();