Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I try to detect in C# Comments after ';'
Is it better to use Indexof and Substring or contains and split?
If i use index i get the ';' including all comments behind.
If i use contains and split i only get one comment but its less codelines.
indexComment = 0;
indexComment = line.IndexOf(";");
if (indexComment >= 0)
{
commands = line.Substring(0, indexComment);
comment = line.Substring(indexComment);
}else
{commands = line;}
VS
if (line.Contains(";"))
{
commands = line.Split(';')[0];
comment = line.Split(';')[1];
}else
{commands = line;}
Both codes works as expected but what would you prefer?
This is example code i want to detect
do something ;this line do something
x+5
;the line above add 5 to x
The split seems better because builds the substrings for you.
Just add the Count parameter with 2, so if you have more than one ';', they will all be included in your comment variable.
And use a temporary variable to avoid splitting twice.
if (line.Contains(";"))
{
string[] splitLine = line.Split(new char[] { ';' }, 2);
commands = splitLine[0];
comment = splitLine[1];
}
else
{ commands = line; }
Related
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 2 years ago.
Improve this question
I have a string array with 3 elements like below for example.
string[] stringarray1;
stringarray1 = new string[5]{ “Element 1\n”, “Element 2\n”, “Element 3\n”, “Element 4\n”, “Element 5\nblablablabla” };
Here i need to check last element in string array having unnecessary dynamic text "\nblablablabla", if exists i need to remove(till last of the dynamic text) and replace with "Element 5\n" in last element.
How can I do this?
try this:
for (int i = 0; i < stringarray1.Length; i++)
{
stringarray1[i] = stringarray1[i].Split("\n")[0] + "\n";
}
You can split string with respect to "\n" and take first of it and add "\n" again. This will remove unnessesary characters from your string that i understand.
Instead of checking and replacing unnecessary text, you can replace what ever string you have with expected string
var lastIndex = stringarray1.Length -1;
stringarray1[lastIndex] = $"Element {lastIndex}\n";
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 8 years ago.
Improve this question
I would like to split directory into two parts:
For example
//Hello//Products//App//Images//Room//40//Tulips.jpg
into
//Hello//Products//App
and
//Images//Room..40//Tulips.jpg
var splitOn = "App";
var path = "//Hello//Products//App//Images//Room//40//Tulips.jpg";
var parts = path.Split(new string[] { splitOn }, StringSplitOptions.None);
Console.WriteLine(parts[0] + splitOn);
Console.WriteLine(parts[1]);
In order to split by a word (or in this case folder) you need to wrap the term in a string array before passing it to the String.Split function. Splitting on "App" will also remove "App" from the result, so we concatenate it again before we write it to the console.
First split the string based on the double forward slash and then assign to array.
string path= Hello//Products//App//Images//Room//40//Tulips.jpg
string[] names = path.Split('//');
After this collect the words like this:-
string first_part=names[0] + "//" + names[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 9 years ago.
Improve this question
Why doesn't this regex, constructed like this:
tmprect = string "gg_rct_MyReg1"
regex = #"^\s*set\s+"+tmprect+#"\s*=\s*Rect\s*\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*,\s**(.+)\s*\).*$";
not work for
set gg_rct_MyReg1 = Rect (-704.0 , -352.0, 224.0 , 448.0) //rect 1
What did I do wrong?
///edited:
string findrectcoord = #"^\s*set\s+" + tmprect + #"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$";
StreamReader file3 = new StreamReader(openFileDialog1.FileName);
string line2;
while ((line2 = file3.ReadLine()) != null)
{
Regex foundrectr = new Regex(findrectcoord);
Match foundrectm = foundrectr.Match(line2);
if (foundrectm.Success)
{
MessageBox.Show("YES");
}
}
string:
set gg_rct_MyReg1 = Rect( -704.0 , -352.0, 224.0 , 288.0 ) //JassCode
Not Found
The regex itself, while ugly and inefficient, should work. You do need to assign the string you're adding into the regex before building the regex, though. While we're at it, let's clean it up:
string tmprect = "gg_rct_MyReg1";
Regex regexObj = new Regex(#"^\s*set\s+" + tmprect +
#"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$");
([^,\s]*) matches any number of characters except commas or spaces. That is more specific than .* which will match too much and force the regex engine to backtrack.
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
Example:
If there is a line http://google.com/adi/727412;sz=728x90;ord=$RANDOM? which contains adi in it, wants it to be replaced with http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM? and rest all other text to be same with no change.
Please help
This is a fairly simple task:
string url = #"http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if (url.Contains(#"/adi/"))
{
int pos = url.IndexOf(";ord"); //// Find first occurence of Ord parameter
url = url.Insert(pos, ";click=$CLICK"); //// Insert text at position
}
Edit: To accomplish the task for multiple occurences I used a solution from this thread.
{
string url = "<google.com/adi/727412;sz=728x90;ord=$RANDOM?>; <google.com/adi/727412;sz=300x250;ord=$RANDOM?>";
string searchString = #"/adi/";
int n = 0;
while ((n = url.IndexOf(searchString, n)) != -1)
{
n += searchString.Length;
int pos = url.IndexOf('?', n);
url = url.Insert(pos, ";click=$CLICK");
}
}
string url = "http://google.com/adi/727412;sz=728x90;ord=$RANDOM?";
if(url.Contains("adi")) url = "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?";
string url = "blablablablablahttp://google.com/adi/727412;sz=728x90;ord=$RANDOM?blablabla";
if(url.Contains("adi")) url.Replace("http://google.com/adi/727412;sz=728x90;ord=$RANDOM?", "http://google.com/adi/727412;sz=728x90;click=$CLICK;ord=$RANDOM?");