Losing characters in strings after performing a split with RegEx - c#

I want to split a string into 2 strings,
my string looks like:
HAMAN*3 whitespaces*409991
I want to have two strings the first with 'HAMAN' and the second should contain '409991'
PROBLEM: My second string has '09991' instead of '409991' after implementing this code:
string str = "HAMAN 409991 ";
string[] result = Regex.Split(str, #"\s4");
foreach (var item in result)
{
MessageBox.Show(item.ToString());
}
CURRENT SOLUTION with PROBLEM:
Split my original string when I find whitespace followed by the number 4. The character '4' is missing in the second string.
PREFERED SOLUTION:
To split when I find 3 whitespaces followed by the digit 4. And have '4' mentioned in my second string.

Try this
string[] result = Regex.Split(str, #"\s{3,}(?=4)");
Here is the Demo

Positive lookahead is your friend:
Regex.Split(str, #"\s+(?=4)");

Or you could not use Regex:
var str = "HAMAN 409991 ";
var result = str.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
EXAMPLE
Alternative if you need it to start with SPACESPACESPACE4:
var str = new[] {
"HAMAN 409991 ",
"HAMAN 409991",
"HAMAN 509991"
};
foreach (var s in str)
{
var result = s.Trim()
.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)
.Select(a => a.Trim())
.ToList();
if (result.Count != 2 || !result[1].StartsWith("4"))
continue;
Console.WriteLine("{0}, {1}", result[0], result[1]);
}

That's because you're splitting including the 4. If you want to split by three-consecutive-spaces then you should specify exactly that:
string[] result = Regex.Split(str, #"\s{3}");

Related

Delim Tabs in a string

string s = " 1 16 34";
string[] words = s.Split('\t');
foreach (string word in words)
{
Console.WriteLine(word);
}
I have a string format as shown above, but when I try to deliminate using the escape tab, it just outputs the exact same string in its original format, why is not removing the tabs?
string[] words = s.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
Console.WriteLine(word);
}
I think I fixed it.
It gives me this output.
1
16
34
Which I checked by outputting all 3 in the array to make sure they are separated.
Split on char[0] - this will split on all whitespaces.
StringSplitOptions.RemoveEmptyEntries - will remover empty entries.
var words = myStr.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
Try using split without a argument
string s = " 1 16 34";
string[] words = s.Split();
foreach (string word in words)
{
Console.WriteLine();
}
This will split the string by every whitespace, tab.
Paranoidal solution: split on any white space (space, tab, non-breaking space etc.)
string s = " 1 16 34";
string[] words = Regex
.Matches(s, #"\S+")
.OfType<Match>()
.Select(m => m.Value)
.ToArray();
Regular expressions can well be overshoot, but can help out in case of dirty data.

How to split a string with a certain character without considering the length of the character?

I'm trying to work on this string
abc
def
--------------
efg
hij
------
xyz
pqr
--------------
Now I have to split the string with the - character.
So far I'm first spliting the string in lines and then finding the occurrence of - and the replacing the line with a single *, then combining the whole string and splitting them again.
I'm trying to get the data as
string[] set =
{
"abc
def",
"efg
hij",
"xyz
pqr"
}
Is there a better way to do this?
var spitStrings = yourString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
If i understand your question, this above code solves it.
Use of string split function using the specific char or string of chars (here -) can be used.
The output will be array of strings. Then choose whichever strings you want.
Example:
http://www.dotnetperls.com/split
I'm confused with exactly what you're asking, but won't this work?
string[] seta =
{
"abc\ndef",
"efg\nhij",
"xyz\npqr"
}
\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\n\r = CR + LF // Used as a new line character in Windows
(char)13 = \n = CR // Same as \n
If I'm understanding your question about splitting -'s then the following should work.
string s = "abc-def-efg-hij-xyz-pqr"; // example?
string[] letters = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
If this is what your array looks like at the moment, then you can loop through it as follows:
string[] seta = {
"abc-def",
"efg-hij",
"xyz-pqr"
};
foreach (var letter in seta)
{
string[] letters = letter.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
// do something with letters?
}
I'm sure this below code will help you...
string m = "adasd------asdasd---asdasdsad-------asdasd------adsadasd---asdasd---asdadadad-asdadsa-asdada-s---adadasd-adsd";
var array = m.Split('-');
List<string> myCollection = new List<string>();
if (array.Length > 0)
{
foreach (string item in array)
{
if (item != "")
{
myCollection.Add(item);
}
}
}
string[] str = myCollection.ToArray();
if it does then don't forget to mark my answer thanks....;)
string set = "abc----def----------------efg----hij--xyz-------pqr" ;
var spitStrings = set.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
EDIT -
He wants to split the strings no matter how many '-' are there.
var spitStrings = set.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
This will do the work.

Not Trimming white spaces from a string in c#

Trim() Function Not Working Please Help its not trimming the white space.
string samplestring = "172-6573-4955";
string[] array = samplestring .Split('-');
string firstElem = array.First();
string restOfArray = string.Join(" ", array.Skip(1));
array[1] = restOfArray.Trim(); // providing value "6573 4955"
The scenario is I am splitting string and merging 2nd and last index into one but it's merging with white spaces.
Trim() removes whitespace from the front and back of a string, not in the middle. The whitespaces in the middle are being inserted by the " " provided as the parameter to Join() method. You could provide string.empty instead.
Trim is used to remove all leading and trailing white-space. And you want to trim the white space from middle. You may try like this:
string restOfArray = string.Join("", array.Skip(1));
or better:
string restOfArray = string.Join(string.Empty, array.Skip(1));
instead of
string restOfArray = string.Join(" ", array.Skip(1));
Trim is working as expected. Taken from String.Trim Method:
Removes all leading and trailing white-space characters from the
current String object.
The possible solutions for you are :
string restOfArray = string.Join(string.Empty, array.Skip(1));
Or
array[1] = restOfArray.Replace(" ", string.Empty).Trim();
To just split the first n-let from the rest use an additional array or you will end up with spurious data. And join with the correct separator you used in split(-):
string restOfArray = string.Join("-", array.Skip(1));
string[] result = new string[] { firstElem, restOfArray };
You don't need Trim().
You can simplify your code this way:
string[] splitCode(string code)
{
string[] segments;
segments = code.Split('-');
return new string[] { segments.First(), string.Join("-", segments.Skip(1)) };
}
Trim really isn't need here; you can simply split the string and join the parts you want:
string source = "172-6573-4955";
string[] splitter = new string[] {"-"};
string[] result;
result = source.Split(splitter, StringSplitOptions.None);
string final = (result[1] + result[2]);
Console.Write(final);
Result:
65734955
Try this:
string samplestring = "172-6573-4955";
string[] array = samplestring.Split(new char[] {'-'},2);
string result = array[1].Replace("-","");
Also, for your question about randomly putting spaces, can u pls explain "randomly" here.

How would I split TO extract spaces in C#

Split or Regex.Split is used to extract the word in a sentence(s) and store them in array. I instead would like to extract the spaces in a sentence(s) and store them in array (it is possible that this sentence contains multiple spaces). Is there easy way of doing it? I first tried to split it normally, and then use string.split(theSplittedStrings, StringSplitOptions.RemoveEmptyEntries) however, that did not preserve the amount of spaces that exists.
---------- EDIT -------------
for example. If there is a sentence "This is a test".
I would like to make an array of string { " ", " ", " "}.
---------- EDIT END ---------
Any helps are appreciated.
Thank you.
EDIT:
Based on your edited question, I believe you can do that with simple iteration like:
string str = "This is a test";
List<string> spaceList = new List<string>();
var temp = str.TakeWhile(char.IsWhiteSpace).ToList();
List<char> charList = new List<char>();
foreach (char c in str)
{
if (c == ' ')
{
charList.Add(c);
}
if (charList.Any() && c != ' ')
{
spaceList.Add(new string(charList.ToArray()));
charList = new List<char>();
}
}
That would give you spaces in different elements of List<string>, if you need an array back then you can call ToArray
(Old Answer)
You don't need string.Split. You can count the spaces in the string and then create array like:
int spaceCount = str.Count(r => r == ' ');
char[] array = Enumerable.Repeat<char>(' ', spaceCount).ToArray();
If you want to consider White-Space (Space, LineBreak, Tabs) as space then you can use:
int whiteSpaceCount = str.Count(char.IsWhiteSpace);
This code matches all spaces in the input string and outputs their indexes:
const string sentence = "This is a test sentence.";
MatchCollection matches = Regex.Matches(sentence, #"\s");
foreach (Match match in matches)
{
Console.WriteLine("Space at character {0}", match.Index);
}
This code retrieves all space groups as an array:
const string sentence = "This is a test sentence.";
string[] spaceGroups = Regex.Matches(sentence, #"\s+").Cast<Match>().Select(arg => arg.Value).ToArray();
In either case, you can look at the Match instances' Index property values to get the location of the space/space group in the string.

How to separate string to more than one part C#

I have a string, that contains links. Example:
www.google.com;www.yahoo.com;www.gmail.com
My question is how can I separate those links so I can add to all the links the tag <a> and in the end of the link the tag </a>?
I should get this:
<a>www.google.com</a>;<a>www.yahoo.com</a>;<a>www.gmail.com</a>
I will be thankful if the solution will be simple as possible and use the IndexOf method.
That code should do the job:
var input = "www.google.com;www.yahoo.com;www.gmail.com";
var result = string.Join(";", input.Split(';').Select(x => string.Format("<a>{0}</a>",x)));
var links = "www.google.com;www.yahoo.com;www.gmail.com";
var result = String.Join(";", links.Split(';').Select(s => String.Format("<a>{0}</a>", s)));
Try something like this:
var result = String.Join(";",
"www.google.com;www.yahoo.com;www.gmail.com"
.Split(';')
.Select(str => String.Format("<a>{0}</a>", str)));
The easiest way:
var result = "<a>" + String.Join("</a>;<a>", input.Split(new char[] { ';' })) + "</a>";
However, it will return <a></a> for empty input.
Explanation:
input.Split(new char[] { ';' }) splits input string by : character.
String.Join("</a>;<a>", input.Split(new char[] { ';' })) joins elements from the split using </a>;<a> string.
"<a>" + String.Join("</a>;<a>", input.Split(new char[] { ';' })) + "</a>"; adds additional <a> in front and </a> at the end of results.
Use split() function . Split string by character ; and store in an array.
string[] arr = inputstring.Split(';');
string outputstring=string.Empty;
for(int i=0;i<arr.Length;i++)
outputstring += "<a>"+arr[i]+"</a>;";
Since you don't want semicolon at end
outputstring = outputstring .TrimEnd(';');

Categories

Resources