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=5454gffdgfg454hg";
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.
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 6 years ago.
Improve this question
My sentence is: !doar 12345, rayantt
Using string.Contains(!doar), I need get the other values as:
int mount = 12345;
string destiny = "rayannt";
Let the input be the input string as you stated in the question, searchString be the string that you wanted to search for; strParam and intParam are the two required outputs; Now consider the following code:
string input = "!doar 12345, rayantt";
string searchString = "!doar";
string strParam=string.Empty ;
int intParam=0;
if (input.Contains(searchString)) // check for the existence of the search string in given string
{
input = input.Replace(searchString, ""); // remove the searchstring from the input
string[] contents = input.Split(',');
int.TryParse(contents[0], out intParam); // collect the integer param
strParam=contents[1]; // collect the string param
}
// here you get 12345 in intParam and "rayantt" in strParam
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
Hi I am facing problem to get specific string. The string as below:
string myPurseBalance = "Purse Balance: +0000004000 556080";
I want to get 4000 out only.
if your string format/pattern is fix then you can get specific value
string myPurseBalance = "Purse Balance: +0000004000 556080";
//
var newPursebal =Convert.ToDouble(myPurseBalance.Split('+')[1].Split(' ')[0]);
You can use this regular expression:
string extract = Regex.Replace(myPurseBalance, #"(.*?)\: \+[0]*(?<val>\d+) (.*)", "${val}")
It searches for the decimals after :, trims the leading 0s and removes everything after the last whitespace.
You can use string.Split to get the +0000004000 and then use string.Substring to get the last four characters by passing the Length-4 as starting index.
string str = myPurseBalance.Split(' ')[2];
str = str.Substring(str.Length-4);
Learn Regex . Here is just simple tutorial
using System;
using System.Text.RegularExpressions;
namespace regex
{
class MainClass
{
public static void Main (string[] args)
{
string input = "Purse Balance: +0000504000 556080";
// Here we call Regex.Match.
Match match = Regex.Match(input, #"\+[0]*(\d+)",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
}
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(",,","");