Move the selected string to first in comma separated string c# [closed] - c#

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
Move the selected string to first in comma separated string c#
If my csvstring is : C1,C2,C3
if my selectedstring is : C2
I require an output of : C2,C1,C3

Using the expression power of LINQ:
string cvsstring = "C1,C2,C3";
string selected = "C2";
cvsstring = string.Join(
",",
new[] { selected }.Concat(cvsstring.Split(',').Except(selected));
If there are more than one selected instances, the duplicated will be removed by this code. As the question is very unprecise about that I won't give a solution (using OrderBy, for example).

You can split, sort and join:
string csv = "C1,C2,C3";
string selected = "C2";
csv = String.Join(',', csv.Split(',').OrderBy(s => s == selected ? 0 : 1));

How about this?
var text ="C1,C2,C3";
var splits = text.Split(',');
var results =
String.Join(",",
Enumerable.Concat(
splits.Where(x => x == "C2"),
splits.Where(x => x != "C2")));

Maybe something like this:
var str= "C1,C2,C3";
var selected="C2";
var result= string.Join(",", str.Split(',').OrderBy (s =>s==selected?0:1));

It is pretty easy. Try below given code:
string csvstring = "C1,C2,C3";
string seletedstring = "C2";
string outputstring = selectedstring + csvstring.Replace(seletedstring .ToString(),"");
outputstring = outputstring.Replace(",,","");

Related

String operations . Change the order and delete dome caracters of a string [closed]

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 3 years ago.
Improve this question
I have a lot of string like this "01/08/2019" and i want to have a string like this "20190801" . I cant use the DateTime format , i must do it using string as a type . Please someone to help
You can use this
string date = "01/08/2019";
string result = string.Empty;
foreach(var item in date.Split('/'))
result = string.Concat(item, result);
what do you mean that you cannot use DateTime format?
Normally you should parse the format, keep it as DateTime in memory and use .ToString(format) for presentation purposes. Doing it all in one line would look like this:
DateTime.ParseExact("01/08/2019", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyyMMdd")
if you surely have '/' as separator,
you can split the string with '/', by this you will get string array.
you can loop on this array in descending order and keep concatenating the element of the array to form one single output.
string dateStr = "01/01/2019";
string[] dateElements = dateStr.Split('/');
string output = string.Empty;
for(int i = dateElements.Length - 1; i >= 0; i--)
{
output += dateElements[i];
}
You can write that code like that:
var input = #"01/08/2019";
var chrs = new[] {'/'};
var result = string.Concat(input.Where(c => !chrs.Contains(c)));
This way:
class Program
{
static void Main(string[] args)
{
string str = "01/08/2019";
string normalizedStr = Normalize(str);
}
private static string Normalize(string str)
{
return string.Join("-", str.Split(new char[] { '/' }).Reverse());
}
}
Basically it split the original string in many strings by char '/'
Then it reverse the order of these strings
Then it concatenate by using "-" as separator

how do you take a string then organize the words in it with how many times the word occurs in c#? [closed]

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);
}
}

how to find in between Ou=9999998 Only number [closed]

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
How to find substring (Ou=9999998 only number) from string?
For example, suppose this is my string:
string myString = "cn=54445sddsfsd,ou=9998fgfgfgf8855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454‌​hg";
I want to only 99988855
I suggest converting the string to dictionary with a help of Linq:
using System.Collections.Generic; // for Dictionary
using System.Linq; // Linq: ToDictionary
...
String source =
"cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg.subject=5454gffdgfg454hg";
Dictionary<String, String> data = source
.Split(',')
.ToDictionary(line => line.Substring(0, line.IndexOf('=')),
line => line.Substring(line.IndexOf('=') + 1));
then get the value
// "99988855"; you may want to put int.Parse(data["ou"]); if you want integer value
String result = data["ou"];
or check if there's value and get it if it is:
String result;
if (data.TryGetValue("ou", out result)) {
// "ou" key found
}
else {
// no "ou" key found
}
Try this way you will get all the number part from this string after equal to symbol in an arry.
string myString = "cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454hg";
string[] myArray= myString.Split(',');
string[] finalOutPut = (from item in myArray
where Regex.IsMatch(item.Substring(item.LastIndexOf('=') + 1), #"^\d+$")
select item.ToString()).ToArray<string>();
In this code the array finalOutput will have one value ie) ou=99988855.

How to format a very long text on a TextBox? [closed]

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
How to format my text in TextBox?
My text value is:
00010001008002020100010000530997000014820000148200010000012C00001482000014820000148200010000012C000014820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000000000000000000000000000000000000003F
And I want my output to be like this:
00010001-0080-02020100010000-53099700-00148200-00-14820001-0000-012C0000-14820000-1482000-0148-20001000-0012C000-0148-20000000-00000000-0000-00-000000000-00000000-0000-00000000-0000000-0000-00000000-00000000-00000000-0000-00000000-00-00000-000001010-00000000-000000000-0000-000000000-00000003-F
Your format must be fixed. It should not be dynamic.
I am just providing a logic you could append the further detail in regex string.
string mystring = "000100010080"
string regex = #"(\w{4})(\w{4})(\w{4})";
string strValue = Regex.Replace(mystring, regex, #"$1-$2-$3");
OUTPUT:
0001-0001-0080
EDITED: Take a look at complete example
string[] patern = "XXXXXXXX-XXXX-XXXXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XX-XXXXXXXX-
XXXX-XXXXXXXX-XXXXXXXX-XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXX-
XXXXXXXX-XXXXXXXX-XXXX-XX-XXXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-
XXXXXXX-XXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX-XXXXXXXX-XX-
XXXXX-XXXXXXXXX-XXXXXXXX-XXXXXXXXX-XXXX-XXXXXXXXX-XXXXXXXX-
X".Split('-');
string mystring = "00010001008002020100010000530997000014820000148200010000012C0
0001482000014820000148200010000012C00001482000000000000000000
0000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000001010000000000000000000000000000
00000000003F";
string regex = string.Empty;
string match = string.Empty;
for(int i=0; i<patern.Length;i++)
{
regex += #"(\w{" + patern[i].Length + "})";
match += "$" + (i + 1).ToString() + "-";
}
match = match.Substring(0, match.Length - 1);
txtMyTextBox.Text = Regex.Replace(mystring, regex, match);
Assuming that you have a fixed pattern of inserting hyphens and the string length is same every time, then you could do something like this:
int[] indices = new int[] { 2, 5, 11 };
string yourLongString = "blahblahblah";
foreach( var index in indices.Reverse() )
{
yourLongString = yourLongString.Insert( index - 1, "-" );
}
OR
Assuming you have no predefined pattern, you could insert hyphens anywhere and hence still you could use the same code above with the tweak to randomize the indices array, if needed.

how can i remove "/ ONLY" from "Rs1500/ONLY",using c# [closed]

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
string oldstring = textBox9.Text;
string newstring = oldstring.Remove(0, 2);
string o = newstring.Remove(4, 7);
Now, I want to get only "1500",rest of the things to be removed.
How can I do this? Please help me.
Try the following code
string newstring = Regex.Replace(oldstring, #"[^\d]", "");
It shall work.
there are number of ways to do this.. you can use Split() as below:
string oldstring = "Rs1500/ONLY";
string[] newstring = oldstring.Split('/');
string o = newstring[0];
This will give you "RS 1500" as result.
And if you want to remove RS as well then just add the below code in last of the above code:
string final = newstring[0].ToString().Replace("Rs","");
That's All
You can use Replace
string oldstring = textBox9.Text;
string newstring = oldstring.Replace("Rs","").Replace("/ONLY","");
This should give you only 1500
Try this way
string ss = "Rs1500/ONLY";
string[] newss = ss.Split('/');
ss = newss[0].ToString().Replace("Rs","");//Or try string.Empty() for instead of ""
See this demo code : with output

Categories