Split on a space or line break - c#

I'm using a data capture software (PSI-Capture) that uses C# scripting. I need to know the best way to convert a captured value to a string and then split on a space or line break character. I'm capturing a full name field (FirstName MiddleName Surname) and sometimes they are in one line or two lines. So I need to split on both spaces and line breaks so I can populate each part of the name in a separate field.
The names are sometimes like that in one line:
MICHAEL JACKSON
And sometimes they are like that in two lines:
MICHAEL JAMES
JACKSON

You can replace the NewLine with space and split on basis of space
string str = str.Replace(Environment.NewLine," ");
string[] array = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Use of StringSplitOptions.RemoveEmptyEntries will ensure that no empty entries remain in array

Let nameInputText be the user input then you can try the following for getting the required array of user name parts:
string actualName = Regex.Replace(nameInputText, #"\r\n?|\n|\s+", "-");
string[] partsOfName = actualName.Split(new char[]{'-'}, StringSplitOptions.RemoveEmptyEntries);
Here in this example, you will get MICHAEL-JAMES-JACKSON in actualName variable and by splitting based on - will gives you the required array of elements, the parts of the name

This might do the trick for you
names = names.Replace(Environment.NewLine, " "); // as suggested by #Dmitry Bychenko
string[] nm = names.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
now if the string[] nm contains 2 elements that means the name doesnt have the middle name so
firstname = nm[0];
if(nm.Length > 2)
{
middlename = nm[1];
lastname = nm[2]
}
else
{
lastname = nm[1]
}

Related

C# check one count of a splitstring and print another count if correct

So I'm currently working on a program that reads a large txt file line by line as strings. For a particular section of the file the format is like this:
text1 : text2 : text3 : text4 : text5 : text6
I know how to split the string and find different counts.
What I want to do is check that the text in text5 starts with a certain expression SORT, and then for that line print text3.
foreach (string str in File.ReadLines(#"/filelocation"))
{
if (str.StartsWith("fusedef"))
{
string text3 = str.Split(':')[2];
string text5 = str.Split(':')[4];
if (text5.StartsWith("SORT_"))
{
Console.WriteLine(text3);
}
}
(As far as I know, counting a split string starts at 0 but correct me if I'm wrong, only started c# a few weeks ago. Thanks!)
You need to remove any char that could potentially confuse the StartsWith. In particular those empty spaces before the string start.
There is an overload of string.Split that allows you to set more than one char to split on and then remove eventually empty strings returned by this split
string[] blocks = str.Split(new char[] {':', ' '}, StringSplitOptions.RemoveEmptyEntries);
if (blocks[4].StartsWith("SORT_"))
{
Console.WriteLine(blocks[2]);
}
In alternative you could Trim the blocks strings
string[] blocks = str.Split(':');
if (blocks[4].Trim().StartsWith("SORT_"))
{
Console.WriteLine(blocks[2]);
}
Splitting the string will output a string array. To really make use of C# and how simple you can make things why not try operating on your string using a List?
If you know the order that items will appear in you can just call up a string from any position in your list.
foreach (string str in File.ReadLines(#"/filelocation")) {
if (str.StartsWith("fusedef")) {
List<string> list = str.Split(':').ToList();
if (list[5].StartsWith("SORT_"))
Console.WriteLine(list[3]);
}
}
Hope this helps!
Edit: you may want to remove the leading and trailing spaces from your separator. or split your string using ' : ' rather than ':'.

C# Coding using strings in order to find a full name

I am using Visual studio to create a C# windows form that helps me find the suffix,first and last name of the user. I am using string.split to find the first space and split from there but it only gives me from the first space onward. if the user input " Mr. Donald duck " I can not manage to make it work in the situation.
"Mr. -5 spaces- Donald -5spaces- Duck"
the code doesn't read past the first space.
any suggestions?
Trimming is only going to take care of leading and trailing white-space characters. Here's what you need in order to get just the 3 useful parts of the text when you have all those extra spaces between words:
string name = "Mr. Donald Duck";
string[] split = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
The string array will contain 3 items: Mr., Donald, and Duck. The StringSplitOptions.RemoveEmptyEntries will take care of repeating white-space when you split the original string.
Without it, you get something like this: Mr., , , , , Donald, , , , , Duck
You should always use String.Trim() function. (To remove leading and trailing white-space from string) when you deal with user input as a string.
string s = " Mr. Donald duck ";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Trim().Split(' ');
//.....check size of array.
if(words.Length ==3)
{
string suffix=words[0];
string firstname=words[1];
string lastname=words[2];
}
I am not getting -5 in your question but hope this will help.
Split with remove empty string option, then you will get non empty word array as result. From that you can get name parts.
Demo
The Syntax for String.Split would be like this:
// 0 1 2
// ooo|oooooo|oooo
string str = "Mr. Donald Duck";
string suffix = str.Split(' ')[0];
string fname = str.Split(' ')[1];
string lname = str.Split(' ')[0];
Just for explanation
According to MSDN You can easily remove white spaces from both ends of a string by using the String.Trim method. You can read it here. For more good understanding you can visit here
string input = Console.ReadLine();
// This will remove white spaces from both ends and split on the basis of spaces in string.
string[] tokens = input.Trim().Split(' ');
string title = tokens[0];
string firstname = tokens[1];
string secondname = tokens[2];

How to Read Multiple Inputs separated by a space from one input line?

Let's say I have a program that asks for the Full Name:
string firstName;
string surName;
Console.Write("Enter your full name:");
let's say the user inputs the string "Santa Clause"
if I use:
firstName = Console.ReadLine();
"Santa Clause" will be stored to firstName, but I only want the "Santa" part.
Is there a way to read only the first word when two or more words are separated by a space? Is there also a way to read the other words (like "Clause")?
Is there a way to read only the first word when two or more words are
separated by a space?
You can use String.Split() method.
Returns a string array that contains the substrings in this instance
that are delimited by elements of a specified Unicode character array.
Like;
string firstName = "Santa Clause";
string[] splitedNames = firstName.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(splitedNames[0]);
Output will be;
Santa
Here is a DEMO.
Is there also a way to read the other words (like "Clause")?
Sure, since String.Split return a string array, you can find the other words using the index number of array like splitedNames[1], splitedNames[2], etc..
try Console.ReadLine().Split(' ') which will give you string[]
Use the string.Split() method:
var fullName = Console.ReadLine()
firstName = fullName.Split(' ')[0];
surName = fullName.Split(' ')[1];
The String.Split Method is your friend here:
string full = "Santa Clause";
string[] parts = full.Split(' ');
string first = parts[0];
string last = parts[1];
var fullName = Console.ReadLine();
fullName = fullName.Split(' ')[0];
var firstName = fullName[0];
var surName = fullname[1];
This is assuming the user enters correct input such as "Santa Clause" otherwise the application will fall over.

Splitting a String into only 2 parts

I want to take a string from a textbox (txtFrom) and save the first word and save whatever is left in another part. (the whatever is left is everything past the first space)
Example string = "Bob jones went to the store"
array[0] would give "Bob"
array[1] would give "jones went to the store"
I know there is string[] array = txtFrom.Split(' '); , but that gives me an array of 6 with individual words.
Use String.Split(Char[], Int32) overload like this:
string[] array = txtFrom.Text.Split(new char[]{' '},2);
http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
You simply combine a split with a join to get the first element:
string[] items = source.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string firstItem = items[0];
string remainingItems = string.Join(" ", items.Skip(1).ToList());
You simply take the first item and then reform the remainder back into a string.
char[] delimiterChars = { ' ', ',' };
string text = txtString.Text;
string[] words = text.Split(delimiterChars, 2);
txtString1.Text = words[0].ToString();
txtString2.Text = words[1].ToString();
There is an overload of the String.Split() method which takes an integer representing the number of substrings to return.
So your method call would become: string[] array = txtFrom.Text.Split(' ', 2);
You can also try RegularExpressions
Match M = System.Text.RegularExpressions.Regex.Match(source,"(.*?)\s(.*)");
M.Groups[1] //Bob
M.Groups[2] // jones went to the store
The regular expression matches everything up to the first space and stores it in the first group the ? mark tells it to make the smallest match possible. The second clause grabs everything after the space and stores it in the second group

Get a file/directory path from a string that contains other data

How would I get the directory and filename from the following string using C#:
string test = "test#test.com, snap555.jpg, c:\users\test\desktop\snap555.jpg";
I only want to be able to get the "c:\users\test\desktop\snap555.jpg" from the string and turn it into another string.
The characters before the "," will always be different and different lengths such as: "bob#bob.com, x.txt, c:\users\test\desktop\x.txt"
What is the easiest way to do this in c#?
Thanks.
t
If the comma delimiter does not appear in the first part, you can use:
string pathName = test.Split(',')[2];
If that space after the comma is a problem and assuming that the first part never has spaces, you can use:
char[] delims = new char[] { ',', ' '};
string pathName = test.Split(delims, StringSplitOptions.RemoveEmptyEntries)[2];
This example assumes you always want the third item, as mentioned in your question. Otherwise, change the 2 in the examples above to the correct index of the item you want. For example, if it should always be the last item, you can do:
char[] delims = new char[] { ',', ' '};
string[] items = test.Split(delims, StringSplitOptions.RemoveEmptyEntries);
string pathName = items[items.Length-1];
If there's always going to be a comma there you can use
string test = "snap555.jpg, c:\users\test\desktop\snap555.jpg";
string[] parts = test.Split(',');
Then get whatever you need from the parts array.

Categories