Split special char character - c#

I have this string that is delimited with char 1 character:
3/1k
this js script may help to see the string
var s="3/1k";
alert(s);
I tried with
string[] s1 = Str.Split(new Char[] { (char)1 },StringSplitOptions.None);
string[] s2 = Str.Split((char)1);
string[] s3 = Str.Split('');//copy and paste of (char)1
string[] s4 = System.Text.RegularExpressions.Regex.Split( Str((char)1).ToString());
I'd like to split it with delimiter (char 1)
So I should get an array with
s[0]="3/1";
s[1]="";
s[2]="";
s[3]="k";
How can I do it with C#?

I think you have to use '1' instead of (char)1
public static void Main(string[] args){
string str = "3/1k";
string[] v = str.Split('1');
foreach(string i in v)
Console.WriteLine(i);
}
Hope this helps!

Both of the first two suggestions you suggest work fine for me if I start with:
var Str = "3/1\u0001\u0001\u0001k";
It would seem that perhaps U+0001 is not actually present in the compiled string. If you are putting control characters into source (rather than e.g. reading them from a file) then it is better to escape them as per C# character escapes, rather than depend upon unprintables in the C# file.

Related

C# Concat multiline string

I got two strings A and B.
string A = #"Hello_
Hello_
Hello_";
string B = #"World
World
World";
I want to add these two strings together with a function call which could look like this:
string AB = ConcatMultilineString(A, B)
The function should return:
#"Hello_World
Hello_World
Hello_World"
The best way to do this for me was splitting the strings into an array of lines and then adding all lines together with "\r\n" and then returning it. But that seems bad practice to me since mulitple lines are not always indicated with "\r\n".
Is there a way to do this which is more reliable?
For a one line solution:
var output = string.Join(System.Environment.NewLine, A.Split('\n')
.Zip(B.Split('\n'), (a,b) => string.Join("", a, b)));
We split on \n because regardless of whether it's \n\r or just \n, it will contain \n. Left over \r seem to be ignored, but you can add a call to Trim for a and b if you feel safer for it.
Environment.NewLine is a platform-agnostic alternative to using "\r\n".
Environment.NewLine might be helpful to resolve the "mulitple lines are not always indicated with "\r\n""-issue
https://msdn.microsoft.com/de-de/library/system.environment.newline(v=vs.110).aspx
Edit:
If you dont know if multiple lines are separated as "\n" or "\r\n" this might help:
input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
Empty lines are removed. If you dont want this use: StringSplitOptions.None instead.
See also here: How to split strings on carriage return with C#?
This does as you asked:
static string ConcatMultilineString(string a, string b)
{
string splitOn = "\r\n|\r|\n";
string[] p = Regex.Split(a, splitOn);
string[] q = Regex.Split(b, splitOn);
return string.Join("\r\n", p.Zip(q, (u, v) => u + v));
}
static void Main(string[] args)
{
string A = "Hello_\rHello_\r\nHello_";
string B = "World\r\nWorld\nWorld";
Console.WriteLine(ConcatMultilineString(A, B));
Console.ReadLine();
}
Outputs:
Hello_World
Hello_World
Hello_World
I think a generic way is very impossible, if you will load the string that is created from different platforms (Linux + Mac + Windows) or even get strings that contain HTML Line Breaks or what so ever
I think you will have to define the line break you self.
string a = getA();
string b = getB();
char[] lineBreakers = {'\r', '\n'};
char replaceWith = '\n';
foreach(string lineBreaker in lineBreakers)
{
a.Replace(lineBreaker, replaceWith);
b.Replace(lineBreaker, replaceWith);
}
string[] as = a.Split(replaceWith);
string[] bs = a.Split(replaceWith);
string newLine = Environment.NewLine;
if(as.Length == bs.Length)
{
return as.Zip(bs (p, q) => $"{p}{q}{newLine }")
}

I want to create an array of words from a txt document in C#.

The Split method returns an error.
static void Main(string[] args)
{
string[] ebook = File.ReadLines("C:\\Users\\Michael\\Downloads\\Documents\\x.txt").ToArray();
string[] words = ebook.Split(' ');
}
Not File.ReadLines(...).Split(' ') but ReadAllText(...).Split()
string[] words = File.ReadAllText(path).Split();
You can use the Split() to split by every white-space including new-line characters or tabs.
You should use File.ReadAllText:
var ebook = File.ReadAllText("C:\\Users\\Michael\\Downloads\\Documents\\x.txt");
var words = ebook.Split(' ');
You were using File.ReadLines that returns an IEnumerable<string> representing each line of your .txt, but if you want to split all words it is more "comfortable" to split from a single string, and File.ReadAllText returns a string with all the text in your file.
File.ReadAllText documentation

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.

Extract the last word from a string using C#

My string is like this:
string input = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API";
Here actually I want to extract the last word, 'API', and return.
What would be the C# code to do the above extraction?
Well, the naive implementation to that would be to simply split on each space and take the last element.
Splitting is done using an instance method on the String object, and the last of the elements can either be retrieved using array indexing, or using the Last LINQ operator.
End result:
string lastWord = input.Split(' ').Last();
If you don't have LINQ, I would do it in two operations:
string[] parts = input.Split(' ');
string lastWord = parts[parts.Length - 1];
While this would work for this string, it might not work for a slightly different string, so either you'll have to figure out how to change the code accordingly, or post all the rules.
string input = ".... ,API";
Here, the comma would be part of the "word".
Also, if the first method of obtaining the word is correct, that is, everything after the last space, and your string adheres to the following rules:
Will always contain at least one space
Does not end with one or more spaces (in case of this you can trim it)
Then you can use this code that will allocate fewer objects on the heap for GC to worry about later:
string lastWord = input.Substring(input.LastIndexOf(' ') + 1);
However, if you need to consider commas, semicolons, and whatnot, the first method using splitting is the best; there are fewer things to keep track of.
First:
using System.Linq; // System.Core.dll
then
string last = input.Split(' ').LastOrDefault();
// or
string last = input.Trim().Split(' ').LastOrDefault();
// or
string last = input.Trim().Split(' ').LastOrDefault().Trim();
var last = input.Substring(input.LastIndexOf(' ')).TrimStart();
This method doesn't allocate an entire array of strings as the others do.
string workingInput = input.Trim();
string last = workingInput.Substring(workingInput.LastIndexOf(' ')).Trim();
Although this may fail if you have no spaces in the string. I think splitting is unnecessarily intensive just for one word :)
static class Extensions
{
private static readonly char[] DefaultDelimeters = new char[]{' ', '.'};
public string LastWord(this string StringValue)
{
return LastWord(StringValue, DefaultDelimeters);
}
public string LastWord(this string StringValue, char[] Delimeters)
{
int index = StringValue.LastIndexOfAny(Delimeters);
if(index>-1)
return StringValue.Substring(index);
else
return null;
}
}
class Application
{
public void DoWork()
{
string sentence = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API";
string lastWord = sentence.LastWord();
}
}
var lastWord = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Last();
string input = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API";
var a = input.Split(' ');
Console.WriteLine(a[a.Length-1]);

Separating a string into substrings

I want to separate a string consisting of one or more two-letter codes separated by commas into two-letter substrings and put them in a string array or other suitable data structure. The result is at one point to be databound to a combo box so this needs to be taken into account.
The string I want to manipulate can either be empty, consist of two letters only or be made up by multiple two-letter codes separated by commas (and possibly a space).
I was thinking of using a simple string array but I'm not sure if this is the best way to go.
So... what data structure would you recommend that I use and how would you implement it?
Definitely at least start with a string array, because it's the return type of string.Split():
string MyCodes = "AB,BC,CD";
char[] delimiters = new char[] {',', ' '};
string[] codes = MyCodes.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
Update: added space to the delimiters. That will have the effect of trimming spaces from your result strings.
Would something like this work?
var list = theString.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
My answer is "right", but I suggest Joel Coehoorn's answer.
public static string[] splitItems(string inp)
{
if(inp.Length == 0)
return new string[0];
else
return inp.Split(',');
}
If you are simply going to bind to the structure then a String[] ought to be fine - if you need to work with the data before you use it as a data source then a List<String> is probably a better choice.
Here is an example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
String s = "ab,cd,ef";
// either a String[]
String[] array = s.Split(new Char[] {','});
// or a List<String>
List<String> list = new List<String>(s.Split(new Char[] { ',' }));
}
}

Categories

Resources