Get array of words from a string [duplicate] - c#

This question already has answers here:
c# split string and remove empty string
(6 answers)
Regex : how to get words from a string (C#)
(6 answers)
Closed 2 years ago.
I am working on an requirement where I need to get all the words from a string into an array.
A 'word' is described as any sequence of non-space charachters.
There can be any number of whitespace charachters present in the string.
Input Examples :
" Hello World!! "
"Hello World!!"
" Hello World!! "
In all above cases the output should be ["Hello","World!!"]
Now I have tried to solve the example myself and have below code :
public string[] GetWords(string s)
{
s=s.Trim();
while(s.Contains(" "))
{
s = s.Replace(" ", " ");
}
string[] input=s.Split(' ');
return input;
}
I am getting correct result using the above code. My concerns is there any way the code can be made clean or more optimized than it currently is.

Use the following code snippet:
var str = " Hello World!! ";
string[] array = str.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
The result will be ["Hello","World!!"]
Note: new Char[] { ' ' } if multiple characters need to be handled. Otherwise, you can use the following
string[] array = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);

I would use a regular expression:
using System.Text.RegularExpressions;
...
public string[] GetWords(string s) =>
Regex.Matches(s, #"[^\s]+")
.Cast<Match>()
.Select(x => x.Value)
.ToArray();

Related

c# how to append arrays without duplicates [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Closed 19 days ago.
The below example splits two strings of codes and appends them together. The problem is it doesn't filter duplicates. In this case allCodes has "DL" twice. Is there a way to do the split & concatenate without dups?
string carCodes = "DL, UL, AL, ";
string airCodes = "RL, DL";
string[] allCodes = airCodes
.Replace(" ", "")
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Concat(carCodes
.Replace(" ", "")
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
A distinct does it.
.Distinct().ToArray();

How to split a string by multiple chars? [duplicate]

This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 6 years ago.
I have a string like this: string ip = "192.168.10.30 | SomeName".
I want to split it by the | (including the spaces. With this code it is not possible unfortunately:
string[] address = ip.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
as this leads to "192.168.10.30 ". I know I can add .Trim() to address[0] but is that really the right approach?
Simply adding the spaces(' | ') to the search pattern gives me an
Unrecognized escape sequence
You can split by string, not by character:
var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);
The Split method accepts character array, so you can specify the second character as well in that array. Since you ware used RemoveEmptyEntries those spaces will be removed from the final result.
Use like this :
string[] address = ip.Split(new char[] { '|',' '}, StringSplitOptions.RemoveEmptyEntries);
You will get two items in the array
"192.168.10.30" and SomeName
This might do the trick for you
string[] address = ip.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();

What is the proper way to split a string by regex [duplicate]

This question already has answers here:
How to keep the delimiters of Regex.Split?
(6 answers)
Closed 6 years ago.
I have a text string with a custom macro:
"Some text {MACRO(parameter1)} more text {MACRO(parameter2)}"
In order to process the macro I want to split the string like that:
expected result
"Some text "
"{MACRO(parameter1)}"
" more text "
"{MACRO(parameter2)}"
I've tried to split the string using Regex.Split()
public static string[] Split(string input)
{
var regex = new Regex(#"{MACRO\((.*)\)}");
var lines = regex.Split(input)
return lines;
}
However Regex.Split() deletes the match itself and gives me the following:
actual result
"Some text "
" more text "
I know I could parse the string doing iterations of .Match() and .Substring()
But is there an easy way get the result along with the matches?
Try this
string input = "Some text {MACRO(parameter1)} more text {MACRO(parameter2)}";
string pattern = #"(?'text'[^\{]+)\{(?'macro'[^\}]+)\}";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine("text : '{0}'; macro : '{1}'", match.Groups["text"].Value.Trim(), match.Groups["macro"].Value.Trim());
}
Console.ReadLine();

Concatenate string list component into a string with comma separated [duplicate]

This question already has answers here:
Convert `List<string>` to comma-separated string
(6 answers)
Closed 7 years ago.
I have a list of string (List<String>). I want to concatenate list items and form a new comma separated string. I am knew in C#. Please provide me best possible solutions.
string[] myList = ...
string concatenated = string.Join("," myList);
In case you need to create CSV (Comma Separated Value) string
List<String> source = new List<String>() {
"abc", // just a string
"d,e", // string which contains comma
"x\"yz", // string which contains quotation
};
String result = String.Join(",", source
.Select(x => x.Contains(',') || x.Contains('"')
? "\"" + String.Concat(x.Select(c => c == '"' ? "\"\"" : c.ToString())) + "\""
: x));
...
// abc,"d,e","x""yz"
Console.Write(result);
Note quotations marks added

How can I split a string with a string delimiter? [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.
I have this string:
"My name is Marco and I'm from Italy"
I'd like to split it, with the delimiter being is Marco and, so I should get an array with
My name at [0] and
I'm from Italy at [1].
How can I do it with C#?
I tried with:
.Split("is Marco and")
But it wants only a single char.
string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);
If you have a single character delimiter (like for instance ,), you can reduce that to (note the single quotes):
string[] tokens = str.Split(',');
.Split(new string[] { "is Marco and" }, StringSplitOptions.None)
Consider the spaces surronding "is Marco and". Do you want to include the spaces in your result, or do you want them removed? It's quite possible that you want to use " is Marco and " as separator...
You are splitting a string on a fairly complex sub string. I'd use regular expressions instead of String.Split. The later is more for tokenizing you text.
For example:
var rx = new System.Text.RegularExpressions.Regex("is Marco and");
var array = rx.Split("My name is Marco and I'm from Italy");
Try this function instead.
string source = "My name is Marco and I'm from Italy";
string[] stringSeparators = new string[] {"is Marco and"};
var result = source.Split(stringSeparators, StringSplitOptions.None);
You could use the IndexOf method to get a location of the string, and split it using that position, and the length of the search string.
You can also use regular expression. A simple google search turned out with this
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
string value = "cat\r\ndog\r\nanimal\r\nperson";
// Split the string on line breaks.
// ... The return value from Split is a string[] array.
string[] lines = Regex.Split(value, "\r\n");
foreach (string line in lines) {
Console.WriteLine(line);
}
}
}
Read C# Split String Examples - Dot Net Pearls and the solution can be something like:
var results = yourString.Split(new string[] { "is Marco and" }, StringSplitOptions.None);
There is a version of string.Split that takes an array of strings and a StringSplitOptions parameter:
http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

Categories