I want to extract unique characters from a string. For example:- 'AAABBBBBCCCCFFFFGGGGGDDDDJJJJJJ' will return 'ABCFGDJ'
I have tried below piece of code but now I want to optimize it.
Please suggest if anyone knows.
static string extract(string original)
{
List<char> characters = new List<char>();
string unique = string.Empty;
foreach (char letter in original.ToCharArray())
{
if (!characters.Contains(letter))
{
characters.Add(letter);
}
}
foreach (char letter in characters)
{
unique += letter;
}
return unique;
}
I don't know if this is faster, but surely shorter
string s = "AAABBBBBCCCCFFFFGGGGGDDDDJJJJJJ";
var newstr = String.Join("", s.Distinct());
Another LINQ approach, but not using string.Join:
var result = new string(original.Distinct().ToArray());
I honestly don't know which approach to string creation would be faster. It probably depends on whether string.Join ends up internally converting each element to a string before appending to a StringBuilder, or whether it has custom support for some well-known types to avoid that.
How about
var result = string.Join("", "AAABBBBBCCCCFFFFGGGGGDDDDJJJJJJ".Distinct());
Make sure that you include System.Linq namespace.
Try this
string str = "AAABBBBBCCCCFFFFGGGGGDDDDJJJJJJ";
string answer = new String(str.Distinct().ToArray());
I hope this helps.
if "AAABBBAAA" should return "ABA", then the following does it. Albeit not very fast.
List<char> no_repeats = new List<char>();
no_repeats.Add(s[0]);
for (int i = 1; i < s.Length; i++)
{
if (s[i] != no_repeats.Last()) no_repeats.Add(s[i]);
}
string result = string.Join("", no_repeats);
Related
I have string str = "Join Smith hate meat".
I want to get JoinSmith from this str.
I tried code:
private static string GetFirstWord(string str)
{
return str.Split(' ').Take(2).ToString();
}
This code not working for me.
I tried: return str.Split(' ').FirstOrDefault it get only first part of string Join.
Use
string result = string.Concat(str.Split(' ').Take(2)); // "JoinSmith"
A Fancy combination:
var result = string.Join(String.Empty, str.Split(' ').Take(2));
Takes the first two words, joins them into one string.
Or:
var result = string.Concat(str.Split(' ').Take(2));
Something a little different
var result = new string(TakeAllUntilSecondSpace(str).ToArray());
Yield the characters you want... sometimes this is a good way if you need a lot of control that standard methods don't provide.
private IEnumerable<char> TakeAllUntilSecondSpace(string s)
{
var spaceEncountered = false;
foreach (char c in s)
{
if (c == ' ')
{
if (spaceEncountered) yield break;
spaceEncountered = true;
} else yield return c;
}
}
I'm pulling a string from a MySQL database containing all ID's from friends in this format:
5+6+12+33+1+9+
Now, whenever i have to add a friend it's simple, I just take the the string and add whatever ID and a "+". My problem lies with separating all the ID's and putting it in an array. I'm currently using this method
string InputString = "5+6+12+33+1+9+";
string CurrentID = string.Empty;
List<string> AllIDs = new List<string>();
for (int i = 0; i < InputString.Length; i++)
{
if (InputString.Substring(i,1) != "+")
{
CurrentID += InputString.Substring(i, 1);
}
else
{
AllIDs.Add(CurrentID);
CurrentID = string.Empty;
}
}
string[] SeparatedIDs = AllIDs.ToArray();
Even though this does work it just seems overly complicated for what i'm trying to do.
Is there an easier way to split strings or cleaner ?
Try this:-
var result = InputString.Split(new char[] { '+' });
You can use other overloads of Split
as well if you want to remove empty spaces.
You should to use the Split method with StringSplitOptions.RemoveEmptyEntries. RemoveEmptyEntries helps you to avoid empty string as the last array element.
Example:
char[] delimeters = new[] { '+' };
string InputString = "5+6+12+33+1+9+";
string[] SeparatedIDs = InputString.Split(delimeters,
StringSplitOptions.RemoveEmptyEntries);
foreach (string SeparatedID in SeparatedIDs)
Console.WriteLine(SeparatedID);
string[] IDs = InputString.Split('+'); will split your InputString into an array of strings using the + character
var result = InputString.Split('+', StringSplitOptions.RemoveEmptyEntries);
extra options needed since there is a trailing +
Example:
string input = "super";
string rep = "a";
I want the output same charterers as per given input string length. The output should be "aaaaa".
I dont like to use own FOR or While loops logic, is there any alternatives to accomplish it.
Use the constructor
string output = new string('a', input.Length);
If you want to repeat a real string n-times, you could use Enumerable.Repeat:
string output = string.Join("", Enumerable.Repeat(rep, input.Length));
I use String.Join to concatenate each string with a specified seperator(in this case none).
By Using Regular Expressions
string input = "123";
string rep = "Abc";
string output = Regex.Replace(input , "(.)", rep)
By using LINQ
string output = string.Concat(input.Select(c => rep));
Output
AbcAbcAbc
Just for fun, here is another way:
new string(input.Select(c => 'a').ToArray());
another way :
string input = "super";
string rep = "a";
var newStr = input.Select(x => rep);
Of if you want a solution that's way to big but very easy to understand:
string output = "";
foreach(char a in input) { // == for(int i = 0; i < input.length; i++) {
output += rep;
}
I didn't know about working with the constructor.
It's a very easy solution, but it'll only work with char's. So you can't repeat strings.
string op = "";
for(int i = 0; i < input.Length; i++)
op += rep;
I want to something as simple as turning "this is a test" into
new string[] {"t","h","i","s"," ","i","s"," ","a"," ","t","e","s","t"}
Would I really have to do something like
test = "this is a test".Select(x => x.ToString()).ToArray();
edit: To clarify, I don't want a char array, ideally I want an array of string. I don't really see anything wrong with the above code except for the fact that I would think there is an easier way.
I believe this is what you're looking for:
char[] characters = "this is a test".ToCharArray();
Strings in C# already have a char indexer
string test = "this is a test";
Console.WriteLine(test[0]);
And...
if(test[0] == 't')
Console.WriteLine("The first letter is 't'");
This works too...
Console.WriteLine("this is a test"[0]);
And this...
foreach (char c in "this is a test")
Console.WriteLine(c);
EDIT:
I noticed the question was updated with regards to char[] arrays. If you must have a string[] array, here's how you split a string at each character in c#:
string[] test = Regex.Split("this is a test", string.Empty);
foreach (string s in test)
{
Console.WriteLine(s);
}
Simple!!
one line:
var res = test.Select(x => new string(x, 1)).ToArray();
Try this:
var charArray = "this is a test".ToCharArray().Select(c=>c.ToString());
You can just use String.ToCharArray() and then treat each char as a string in your code.
Here's an example:
foreach (char c in s.ToCharArray())
Debug.Log("one character ... " +c);
Most likely you're looking for the ToCharArray() method. However, you will need to do slightly more work if a string[] is required, as you noted in your post.
string str = "this is a test.";
char[] charArray = str.ToCharArray();
string[] strArray = str.Select(x => x.ToString()).ToArray();
Edit: If you're worried about the conciseness of the conversion, I suggest you make it into an extension method.
public static class StringExtensions
{
public static string[] ToStringArray(this string s)
{
if (string.IsNullOrEmpty(s))
return null;
return s.Select(x => x.ToString()).ToArray();
}
}
Convert the message to a character array, then use a for loop to change it to a string
string message = "This Is A Test";
string[] result = new string[message.Length];
char[] temp = new char[message.Length];
temp = message.ToCharArray();
for (int i = 0; i < message.Length - 1; i++)
{
result[i] = Convert.ToString(temp[i]);
}
string input = "this is a test";
string[] afterSplit = input.Split();
foreach (var word in afterSplit)
Console.WriteLine(word);
Result:
this
is
a
test
I'm trying to fetch multiple email addresses seperated by "," within string from database table, but it's also returning me whitespaces, and I want to remove the whitespace quickly.
The following code does remove whitespace, but it also becomes slow whenever I try to fetch large number email addresses in a string like to 30000, and then try to remove whitespace between them. It takes more than four to five minutes to remove those spaces.
Regex Spaces =
new Regex(#"\s+", RegexOptions.Compiled);
txtEmailID.Text = MultipleSpaces.Replace(emailaddress),"");
Could anyone please tell me how can I remove the whitespace within a second even for large number of email address?
I would build a custom extension method using StringBuilder, like:
public static string ExceptChars(this string str, IEnumerable<char> toExclude)
{
StringBuilder sb = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (!toExclude.Contains(c))
sb.Append(c);
}
return sb.ToString();
}
Usage:
var str = s.ExceptChars(new[] { ' ', '\t', '\n', '\r' });
or to be even faster:
var str = s.ExceptChars(new HashSet<char>(new[] { ' ', '\t', '\n', '\r' }));
With the hashset version, a string of 11 millions of chars takes less than 700 ms (and I'm in debug mode)
EDIT :
Previous code is generic and allows to exclude any char, but if you want to remove just blanks in the fastest possible way you can use:
public static string ExceptBlanks(this string str)
{
StringBuilder sb = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
switch (c)
{
case '\r':
case '\n':
case '\t':
case ' ':
continue;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}
EDIT 2 :
as correctly pointed out in the comments, the correct way to remove all the blanks is using char.IsWhiteSpace method :
public static string ExceptBlanks(this string str)
{
StringBuilder sb = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if(!char.IsWhiteSpace(c))
sb.Append(c);
}
return sb.ToString();
}
Given the implementation of string.Replaceis written in C++ and part of the CLR runtime I'm willing to bet
email.Replace(" ","").Replace("\t","").Replace("\n","").Replace("\r","");
will be the fastest implementation. If you need every type of whitespace, you can supply the hex value the of unicode equivalent.
With linq you can do it simply:
emailaddress = new String(emailaddress
.Where(x=>x!=' ' && x!='\r' && x!='\n')
.ToArray());
I didn't compare it with stringbuilder approaches, but is much more faster than string based approaches.
Because it does not create many copy of strings (string is immutable and using it directly causes to dramatically memory and speed problems), so it's not going to use very big memory and not going to slow down the speed (except one extra pass through the string at first).
You should try String.Trim(). It will trim all spaces from start to end of a string
Or you can try this method from linked topic: [link]
public static unsafe string StripTabsAndNewlines(string s)
{
int len = s.Length;
char* newChars = stackalloc char[len];
char* currentChar = newChars;
for (int i = 0; i < len; ++i)
{
char c = s[i];
switch (c)
{
case '\r':
case '\n':
case '\t':
continue;
default:
*currentChar++ = c;
break;
}
}
return new string(newChars, 0, (int)(currentChar - newChars));
}
emailaddress.Replace(" ", string.Empty);
There are many diffrent ways, some faster then others:
public static string StripTabsAndNewlines(this string str) {
//string builder (fast)
StringBuilder sb = new StringBuilder(str.Length);
for (int i = 0; i < str.Length; i++) {
if ( ! Char.IsWhiteSpace(s[i])) {
sb.Append();
}
}
return sb.tostring();
//linq (faster ?)
return new string(str.ToCharArray().Where(c => !Char.IsWhiteSpace(c)).ToArray());
//regex (slow)
return Regex.Replace(str, #"\s+", "")
}
Please use the TrimEnd() method of the String class. You can find a great example here.
You should consider replacing spaces on the record-set within your stored procedure or query using the REPLACE( ) function if possible & even better fix your DB records since a space in an email address is invalid anyways.
As mentioned by others you would need to profile the different approaches. If you are using Regex you should minimally make it a class-level static variable:
public static Regex MultipleSpaces = new Regex(#"\s+", RegexOptions.Compiled);
emailAddress.Where(x=>{ return x != ' ';}).ToString( ) is likely to have function overhead although it could be optimized to inline by Microsoft -- again profiling will give you the answer.
The most efficient method would be to allocate a buffer and copy character by character to a new buffer and skip the spaces as you do that. C# does support pointers so you could use unsafe code, allocate a raw buffer and use pointer arithmetic to copy just like in C and that is as fast as this can possibly be done. The REPLACE( ) in SQL will handle it like that for you.
string str = "Hi!! this is a bunch of text with spaces";
MessageBox.Show(new String(str.Where(c => c != ' ').ToArray()));
I haven't done performance testing on this, but it's simpler than most of the other answers.
var s1 = "\tstring \r with \t\t \nwhitespace\r\n";
var s2 = string.Join("", s1.Split());
The result is
stringwithwhitespace
string input =Yourinputstring;
string[] strings = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string value in strings)
{
string newv= value.Trim();
if (newv.Length > 0)
newline += value + "\r\n";
}
string s = " Your Text ";
string new = s.Replace(" ", string.empty);
// Output:
// "YourText"
Fastest and general way to do this (line terminators, tabs will be processed as well). Regex powerful facilities don't really needed to solve this problem, but Regex can decrease performance.
new string
(stringToRemoveWhiteSpaces
.Where
(
c => !char.IsWhiteSpace(c)
)
.ToArray<char>()
)