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 4 years ago.
Improve this question
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4.
I suppose I should detect the strings "joined the lobby", and ": " using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.
var list = #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
string sourc = #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\\s]*(Summoner[\\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();
Related
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 3 years ago.
Improve this question
The code is supposed to put all letters into lower case and change j to i, which it does. But I'm trying to take out any duplicate letters.
example inputted string = jjjaaaMMM expected output string = jam
what actually happens real output string = m please help I'm not sure what I'm missing.
string key = Secret.Text;
var keyLow = key.ToLower();
var newKey = keyLow.Replace("j", "i");
var set = new HashSet<char>(newKey);
foreach (char c in set)
{
Secret.Text = Char.ToString(c);
}
Your issue is entirely the = in
Secret.Text = Char.ToString(c);
it needs to be +=
Secret.Text += Char.ToString(c);
You were overwriting each value with the next.
However you could just use linq
Secret.Text = string.Concat(key.ToLower().Replace("j", "i").Distinct());
or probably more efficiently from #Ben Voigt comments
Since you have a sequence of char, it's probably more efficient to
call the string constructor than Concat
Secret.Text = new string(set.ToArray());
// or
Secret.Text = new string(key.ToLower()
.Replace("j", "i")
.Distinct()
.ToArray());
Additional Resources
String.Concat Method
Concatenates one or more instances of String, or the String
representations of the values of one or more instances of Object.
Enumerable.Distinct Method
Returns distinct elements from a sequence.
Others may answer the question directly. I'll provide an alternative. As always with string manipulation, there's a Regex for that.
var output = Regex.Replace(input, #"(.)\1*", "$1");
You can use Linq approach:
var text = key.ToLower().Distinct().ToArray();
Don't forgot add using System.Linq
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 5 years ago.
Improve this question
ok I have no idea on how to do this and i have tried looking up how to do this but nothing good came up so ill ask it here. So what i am trying to do is:
string input = TextEditor.text; <-- this is in windows form application and
The "TextEditor" is the textbox for input
i want to take the string (which is input from the texct box) then split it so each word is on every other line like so:
if input = "hi my name is is"
out put should be:
hi: 1
my: 1
name: 1
is: 2 <-- if the word is said it shouldn't be repeated.
could someone please help me? I am a true newbie and i am completely lost. I don't have any code yet because I DONT KNOW HOW TO DO THIS!
Use Linq GroupBy and Count:
string inputText = "hi my name is is";
var words = inputText.Split(' ').ToList();
var wordGroups = words.GroupBy(w => w).Select(grp => new {
Word = grp.Key,
Count = grp.Count()
});
string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi: 1
my: 1
name: 1
is: 2
*/
Split the input string into an array, then use that to build a dictionary. If the word is already in the dictionary, increment it. Otherwise add it with an initial value of 1.
string input = TextEditor.text;
string[] inputWords = input.Split(' ');
Dictionary<string, int> wordCounts = new Dictionary<string, int>();
foreach (string word in inputWords)
{
if (wordCounts.ContainsKey(word))
{
wordCounts[word]++;
}
else
{
wordCounts.Add(word, 1);
}
}
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 7 years ago.
Improve this question
I have a input string,
string str1 = "aabcccabdfa";
I want to count the occurrence of each character and put it next to that character. So I need to output result as "a4b2c3d1f1". How can i achieve this with LINQ?
I try with "Dictionary" to do that, but not able to do that.
Thanks,
Do it like this:
string str1 = "aabcccabdfa";
string.Concat(str1.GroupBy(c => c).OrderBy(c => c.Key).Select(c => c.Key.ToString() + c.Count()));
string str1 = "aabcccabdfa";
var result=string.Concat(str1.GroupBy(a=>a)
.Select(a=>a.Key.ToString()+a.Count().ToString()));
string str1 = "aabcccabdfa";
var output = "";
str1.ToArray().Distinct().ToList().ForEach(x => output += x + (str1.Count(f => f == x).ToString()));
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 7 years ago.
Improve this question
I have files on a local server with the address of \\localServerAddress\Folder\Program.exe. I need to remove the server address dynamically and replace it with a different server address that is being selected elsewhere in the form. The server names can be different lengths, therefore, I can not use the string.Substring function.
So given the input
\\localServerAddress\Folder\Program.exe
I would like the result
\\differentServerAddress\Folder\Program.exe
If you are always working with UNCs
Then
string toRemove = new Uri(yourString).host;
string newString = yourString.Replace(String.format(#"\\{0})",toRemove)
, String.format(#"\\{0})",whateveryouwant));
Use this method:
string changeServerInPathString(string originalString, string newServer)
{
List<string> stringParts = originalString.TrimStart('\\').Split('\\').ToList();
stringParts.RemoveAt(0);
stringParts.Insert(0, newServer);
return string.Join("\\", stringParts.ToArray()).Insert(0, "\\\\");
}
You can use something like this:
void Main()
{
string path = #"\\localServerAddress\Folder\Program.exe";
UriBuilder bld = new UriBuilder(path);
bld.Host = "NewServer";
Console.WriteLine(bld.Uri.LocalPath);
}
Result: \\newserver\Folder\Program.exe
string text = #"\\test\FolderName\foo.exe";
text = text.Replace('\\', '-'); \\ this is done as I was not able to make the regex **\\\\\\(.)*?\\** , work.
Regex rg = new Regex("--.*?-"); \\ if in case the above mentioned regex is made to work correctly please replace the regex with the same.
text = rg.Replace(text, "");
Console.WriteLine(text.Replace('-', '\\'));
Console.Read();
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 8 years ago.
Improve this question
Need help to find C# code to do a SQL-type like search (case insensitive)
could you please help me with regex code for this. Pattern and test candidates are both user input
* could be anywhere. so pattern could be .T*S.com
e.g.
Pattern = *.test.com
Test Candidate1 = abc.test.com Result = Pass
Test Candidate2 = abc.tESt.com Result = Pass
Test Candidate3 = abc.itest.com Result = FAIL
In case the * is at the front you can use String.EndsWith().
Like
"abc.test.com"
.EndsWidth(".test.com", StringComparison.InvariantCultureIgnoreCase);
returns true.
If you don't want to go the regex route and the candidate always ends with .test.com you can get rid of the * in your pattern and then check with EndsWith:
if (candidate.EndsWith(pattern, StringComparison.InvariantCultureIgnoreCase))
// you have a match
If you want the regex, the pattern is need to be:
public bool IsMatch(string s,string pattern)
{
return System.Text.RegularExpressions.Regex.IsMatch(s, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
string pattern = ".*\.test\.com"
Console.WriteLine(IsMatch("abc.test.com",pattern).ToString()); //PASS
Console.WriteLine(IsMatch("abc.tESt.com",pattern).ToString()); //PASS
Console.WriteLine(IsMatch("abc.itest.com",pattern).ToString()); //FAIL