Split a string with slash [duplicate] - c#

This question already has an answer here:
Splitting the array with / slash
(1 answer)
Closed 7 years ago.
I have a string which includes integers between slashes.
For example:
string myString = "/1//2//56//21/";
I need to take these integers and add them into a list. How can I split this string to its integers?

string myString = "/1//2//56//21/";
int[] arrayInt = Regex.Split(myString, "/+").Where(s => !String.IsNullOrWhiteSpace(s)).Select(Int32.Parse).ToArray();

Related

How to split a string array using char as delimiter [duplicate]

This question already has answers here:
How to split a string by a specific character? [duplicate]
(6 answers)
Closed 10 months ago.
I want to add ' char as delimiter to my string array but I have error telling conversion string to char impossible.
I see this How to split a string by a specific character?
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split("'").ToString();
The issues is that you have the .ToString() after your statement.
Change it to this:
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split('\'');
The reason why this code doesn't work:
string[] splitTest = ineedtosplitthis.Split("'").ToString();
Is because you are converting it to a string via ToString();, while assigning it to a string[].
Split(); already returns a string[], so there is no need to convert it.
Hope this explanation helped :)

Cannot convert string to IEnumerable<string> [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 1 year ago.
I need to convert string to IEnumerable<string> by every newline character.
I just want the same functionality as File.ReadLines without loading any file, rather taking it from a previously loaded string.
String.Split can split your string and give you string[]
Usage
string someString = ...;
string[] lines = someString.Split('\n');
IEnumerable<string> linesAsEnumerable = lines;
DoSomethingWithLines(linesAsEnumerable);
You could split it according to the newline character:
String[] lines = fileContenets.Split('\n');

Replace RX123456789 with RX********* using REGEX [duplicate]

This question already has answers here:
mask all digits except first 6 and last 4 digits of a string( length varies )
(13 answers)
Closed 4 years ago.
I'm using C# to create a pattern in order to replace all the occurrences in a string from
RX123456789 into RX*********
I have tried various patterns without success. I'm kinda new regarding Regular Expression.
I appreciate your help.
Use this Regex:-
string data = "RX123456789";
var resultString = Regex.Replace(data, #"[0-9]", "*");
If you need * of number only When RX is present then use this logic:-
string data = "RX123456789";
var resultString="";
if (new Regex("RX([0-9]+)").IsMatch(data))
{
resultString = Regex.Replace(data, #"[0-9]", "*");
}

Regex to split string into groups [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 4 years ago.
How can I match this string in c# using regex so it returns 4 groups per line?
1 or more digits|one or more letters|one or more letters|one ore more X-Digit(s)\n
Example:
123|ABC|ABC|X-1;X-12;X-13
123|ABC|ABC|X-1
I've tried this
\d+\|(A-Z)\|(A-Z)\|(X-)d+
Why shooting with canons at birds?=! If you could simply use the String.Split method to achieve that
string test = "123|ABC|ABC|X-1;X-12;X-13";
string [] groups = test.Split('|');
it will return an array of elements that were separated by a |

Extract part of the string [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
I have thousands of lines of string type data, and what I need is to extract the string after AS. For example this line:
CASE END AS NoHearing,
What I want would be NoHearing,
This line:
CASE 19083812 END AS NoRequset
What I need would be NoRequset
So far I have tried couple ways of doing it but no success. Can't use .split because AS is not Char type.
If this will be the only way that AS appears in the string:
noRequestString = myString.Substring(myString.IndexOf("AS") + 3);
Using Regex I extract all between the AS and a comma:
string data = #"
CASE END AS NoHearing,
CASE 19083812 END AS NoRequset
";
var items = Regex.Matches(data, #"(?:AS\s+)(?<AsWhat>[^\s,]+)")
.OfType<Match>()
.Select (mt => mt.Groups["AsWhat"])
.ToList();
Console.WriteLine (string.Join(" ", items)); // NoHearing NoRequset

Categories