What's the difference between {"::"} and "::"? [duplicate] - c#

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

Related

how to get rid of Too many characters in character literal? [duplicate]

This question already has answers here:
"Too many characters in character literal error"
(6 answers)
Closed 6 years ago.
I get the error Too many characters in character literal for the below code?how should I declare the buildserverlocation?
using System;
class Program
{
static void Main(string[] args)
{
string buildServerLocation = #'\\Location\builds682\INTEGRATION\ABC1234.QSC.0.0-000000025-P-1';
char delimiterChars = '\\';
string[] serverString = buildServerLocation.Split(delimiterChars);
string serverName = serverString[1] + ".company.com";
Console.WriteLine("build server name is " + serverName);
}
}
This needs to be a string, but you've used the ' syntax, which is valid only for a single character:
string buildServerLocation = #'\\snowcone\builds682\INTEGRATION\IPQ8064.ILQ.5.0-000000025-P-1';
should be
string buildServerLocation = #"\\snowcone\builds682\INTEGRATION\IPQ8064.ILQ.5.0-000000025-P-1";
This declaration is correct by itself, but you then go on to pass it to split. That's legal -- thanks Jeppe -- but it would be better to make it clear that this is an array with a single char:
char delimiterChars = '\\';
should be
char[] delimiterChars = { '\\' };
But the larger problem is that none of this is correct. If you're splitting up a path, use a class dedicated to extracting data from paths rather than rolling your own.
Also, not a super great idea from a security perspective to be posting paths of your internal servers on a public site.
This line is your problem:
char delimiterChars = '\\';
You can only have one character in a char literal. What you need to use as delimiters either needs to be a string denoted by using quotations marks.
string delimiterChars = "\\";
Note: This will work with little other editing, as string.Split() has an overload that takes a string. You just need to define its second parameter, which in this case could be null.

How to Create a string array by spliting a string? [duplicate]

This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 6 years ago.
I am kinda stuck at the moment, i have a string of numbers which i dynamically get them from database, the range of numbers can be between 1 to 1 milion, something like this :
string str = "10000,68866225,77885525,3,787";
i need to create an array from it, i have tried this:
string[] strArr = { str.Replace(",", "").Split(',') };
but it doesnt work anyone has any solution i am all ours. Basically it needs to be like this:
string[] strArr = { "10000","68866225","77885525","3","787" };
Your attend:
string[] strArr = { str.Replace(",", "").Split(',') };
does not work because of two errors in the code:
1) You are removing all , before you are splitting at ,:
str.Replace(",", "")
So basically you are trying to split this string: "1000068866225778855253787" at each ,, which will result in an array containing only "1000068866225778855253787" because obviously there is no , to split on.
2) You are trying to assign an array to a string, because the Split() method already returns an array and you are trying to put this array into a field of string[] (because of the { } around your assignment) and a field of string[] is of type string and not an array.
To get your expected output you have to do the .Split(',') on the original string, which includes all the , on which you are splitting. So just remove the Replace() call and you will get your desired output:
var str = "10000,68866225,77885525,3,787";
var strArr = str.Split(',');
Try this:
string[] strArr = str.Split(',');
The split method already returns an array. Just take it out of the squiggly brackets.
string[] strArr = str.Split(',');
Edit: Sorry forgot to take out the .replace()
The method string.Split() already returns an array.
string[] strArr =yourstring.Split(',');
This will return an array with possible split counts , for example
"10000,68866225,77885525,3,787" will give array with the size of 4.

splitting the string and choosing the middle part containing two set of parenthesis [duplicate]

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.

How to create string from array of strings [duplicate]

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

.Net Split with split characters retained [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
C# split string but keep split chars / separators
Is there a simple way to do a .Net string split() function that will leave the original split characters in the results?
Such that:
"some text {that|or} another".Split('{','|','}');
would result in an array with:
[0] = "some text "
[1] = "{"
[2] = "that"
[3] = "|"
...
Preferably without a regex.
check out this post
the first answer with a RegEx solution, the second for a non-regex solution...
In Concept...
string source = "123xx456yy789";
foreach (string delimiter in delimiters)
source = source.Replace(delimiter, ";" + delimiter + ";");
string[] parts = source.Split(';');
you can probably roll your own using String.IndexOf Method (String, Int32) to find all of your initial separator characters, and merge those in with the results of String.Split

Categories

Resources