This question already has an answer here:
Concatenate string collection into one string with separator and enclosing characters
(1 answer)
Closed 7 years ago.
I have this array:
And I need to make from array above string,one string like this:
(export_sdf_id=3746) OR (export_sdf_id=3806) OR (export_sdf_id=23) OR (export_sdf_id=6458) OR (export_sdf_id=3740) OR (export_sdf_id=3739) OR (export_sdf_id=3742)
Any idea what is the elegant way to implement it?
There is the String.Join-method designed for this.
var mystring = String.Join(" OR ", idsArr);
This will result in the following string:
export_sdf_id=3746 OR export_sdf_id=3806 OR export_sdf_id=23 OR export_sdf_id=6458 OR export_sdf_id=3740 OR export_sdf_id=3739 OR export_sdf_id=3742
Note that the brackets are omited as they are not needed for your query.
You can use String.Join(String, String[]), where first parameter is separator between array elements.
string result = String.Join(" OR ", sArr);
Related
This question already has answers here:
Split string requires array declaration
(2 answers)
Closed 2 years ago.
I have some code like this:
string[] separator = {"::"};
var seperatedCardString = currentCard.Name.Split(
separator, StringSplitOptions.RemoveEmptyEntries);
Can someone explain to me exactly what's happening with this and why there is a need to use {"::"}. My separator is :: so I am confused as to why it's coded the way it is.
The code line string[] separator = {"::"}; is initializing array separator. This syntax to initialize the array is referred as Implicitly Typed Arrays.
Currently your code using Split(String[], StringSplitOptions) method of string to split the string where the first arg is type of string array. If you have only one seperator (i.e. ::) then you can use the overload method Split(String, StringSplitOptions) by below code
string separator = "::";
var seperatedCardString = currentCard.Name.Split(
separator, StringSplitOptions.RemoveEmptyEntries);
Check all the overload of string Split method at here
This question already has an answer here:
How can I substitute the value of one string into another based on a substitution character?
(1 answer)
Closed 3 years ago.
I have code that creates two strings:
var string1 = "ま|ちが|#";
var string2 = "間|違|う";
I'm looking for a way to combine these such the resulting output contains the characters from string1 but if the character is a "#" then it takes the alternate character from string2.
string1 string2 desired output
ま|ちが|# 間|違|う まちがう
な|# 為|る なる
で|き|# 出|来|る できる
Does anyone have any suggestions as to how I can do this?
You can use IndexOf() method like
var hashIndex = string1.IndexOf('#');
if(hashIndex > 0) {
Console.WriteLine(string1.Substring(0, string1.Length - 2) + string2[hashindex]);
}
This question already has an answer here:
convert multiline comma separated textbox values with single quotes
(1 answer)
Closed 5 years ago.
I am trying to convert a list of strings into a comma separated with quotes variable,I can only join them as comma separated but can't put quotes around each of the entries in the list..can anyone provide guidance on how to fix it?
INPUT:
variants =
[
"CI_ABC1234.LA.0.1-03391-STD.INT-32",
"CI_ABC1234.LA.0.1-33103-STD.INT-32"
]
EXPECTED OUTPUT:
('CI_ABC1234.LA.0.1-03391-STD.INT-32','CI_ABC1234.LA.0.1-33103-STD.INT-32')
CODE:-
string variants_str = String.Join(",", variants);
LINQ's Select() extension method allows to convert each item in a collection:
string variants_str = String.Join(",", variants.Select(s => "'" + s + "'"));
Demo: https://dotnetfiddle.net/I37xr6
This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.
This question already has answers here:
How do you remove repeated characters in a string
(7 answers)
Closed 10 years ago.
I have a string given by user. After the user entry i want the character '-' to appear only once even if appears twice or more.
DF--JKIL-L should be DF-JKIL-L
`DF-----JK-L-` should be `DF-JK-L-`
A simple regular expression should do the trick:
string originalString = "DF-----JK-L-";
string replacedString = Regex.Replace(originalString, "-+", "-");
You can use Split with option StringSplitOptions.RemoveEmptyEntries, then Join again:
var result = string.Join("-",
input.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries));