Get a string inside a string - c#

string a = "100-0-6-7-6-10-8-" //and so on
/////////////////////////////////////////////
//my solution
char[] delimiterChars = {'-'};
string solution = "100-0-6-7-6-10-8-";
string[] words = solution.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);
//ok so i encounter some problems when trying to overwrite 0s, and 10s,100s etc
//so when writing data to the "solution" string it should be
//"-100--0--6--7--6--10--8-" instead of "100-0-6-7-6-10-8-"
basically, I want to separate each number and put it in a list or array

i think i got it
char[] delimiterChars = {'-'};
string text = "100-0-6-7-6-10-8-";
string[] words = text.Split(delimiterChars,System.StringSplitOptions.RemoveEmptyEntries);

This solution using Linq works as well.
var text="100-0-6-7-6-10-8-";
var words=text.Split("-").Where(x => !string.IsNullOrEmpty(x)).ToArray();

Related

Unable to split a string using split() method

I have a string
String data="CE|2014-2015|ClassA"
I need output like
string Batch="2014-2015"
string Class="ClassA"
How can I achieve it?? I tried a lot string,Split() function. But I did not get expected output.Please help me out
I tried,
string s = "CE|2014-2015|Class1";
string[] words = s.Split('|| ');
This should work for you
string[] splitted = data.Split('|');
string Batch = splitted[1];
string Class = splitted[2];
Your solution is wrong because: '|| ' is not a valid char and it won't even compile. You should split on | and take second and third value from splitted values
You can do the following
string data = "CE|2014-2015|ClassA";
string[] split = data.Split('|');
string Batch=split[1];
string Class = split[2];
Hope it works for you.

How to get the next word in a string?

I feel pretty foolish for asking a seemingly easy question Sigh but for the life of me I cant figure it out.
string myString = "10/27/14 TheNextString DontWantThisString";
Assume that the second string is unknown(as in it could be any type of word). How could I get the second word after the last index of the date.
Sorry this Is probably a weird question.
var lastLine = line.Substring(idx + "date:".Length + 1, 14);
var lastChar = lastLine.Substring(lastLine.Length-1, 1);
headerName = lastLine.Substring(lastLine.LastIndexOf(lastChar), +1);
Heres some of my code for a little context if you will.
You want String.Split().
string[] delimiters = new string[] {" "};
string[] words = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string myString = "10/27/14 TheNextString DontWantThisString";
var values = Regex.Split(myString , #"\s+");
if (values.Count > 1)
Console.WriteLine(values[1]);
This should work (haven't tested the code, but you get the idea).
String result[] = myString.split(null);
result[1] will return what you need.
See: String.SplitMethod
namespace SplitDemo
{
class Program
{
static void Main(string[] args)
{
var myString = "10/27/14 TheNextString DontWantThisString";
var stringArray = myString.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
var word = stringArray[1];
}
}
}
Something like this should work for you. This splits the string into an array, using a space as the separator, removes any empty matches, and then gets the second item.
myString.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries)[1]
http://msdn.microsoft.com/en-us/library/ms131448.aspx

How to split a string by a specific character? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do i split a String into multiple values?
I have a string 0001-102525 . I want to split it into 0001 and 102525.How can i do it?
Regards,
var myString = "0001-102525";
var splitString = myString.Split("-");
Then access either like so:
splitString[0] and splitString[1]
Don't forget to check the count/length if you are splitting user inputted strings as they may not have entered the '-' which would cause an OutOfRangeException.
How about:
string[] bits = text.Split('-');
// TODO: Validate that there are exactly two parts
string first = bits[0];
string second = bits[1];
You can use C# split method - MSDN
string strData = "0001-102525";
//Using List
List<string> strList = strData.Split('-').ToList();
string first = strList.First();
string last = strList.Last();
//Using Array
string[] strArray = strData.Split('-');
string firstItem = strArray[0];
string lastItem = strArray[1];
string myString = "0001-102525";
//to split the string
string [] split = myString.Split("-");
//to display the new strings obtained to console
foreach(string s in split)
{
if(s.Trim() !="")
Console.WriteLine(s);
}
string strToSplit = "0001-102525"; //Can be of any length and with many '-'s
string[] arrStr = strToSplit.Split('-');
foreach (string s in arrStr) //strToSplit can be with many '-'s. This foreach loop to go thru entire arrStr string array
{
MessageBox.Show(s);
}

line split array on console?

lets take for example i have a string declared:
string master = "1.2.3.4";
which is in the form of:
major.minor.project.build
how do i get a string "major","minor","project" and "build" assigned separately from the string "master"?
I know we have to do line.split and this is what i have tried:
string[] master = line.Split('.');
string major = master[0];
string minor = master[1];
string project = master[2];
string build = master[3];
In this particular case you can use Version class.
var master = "1.2.3.4";
var version = new Version(master);
var major = version.Major;
var minor = version.Minor;
var build = version.Build;
var revision = version.Revision;
Assuming that you want to split the string by '.' to get major, minor, build etc
string master = "1.2.3.4";
string [] arr = master.Split('.');
string major = arr [0];
string minor = arr [1];
string project = arr [2];
string build = arr [3];
Try Like
string master = "1.2.3.4";
string major = master.Split('.')[0];
string minor = master.Split('.')[1].;
string project = master.Split('.')[2];
string build = master.Split('.')[3];
or
string master = "1.2.3.4";
string [] arrayvar = master.Split('.');
string major = arrayvar [0];
string minor = arrayvar [1];
string project = arrayvar [2];
string build = arrayvar [3];
My humild opinion is I don't know what you find wrong in your approach.
I'm going to point you that this is the right way of doing this.
There're other situations where you'll need to identify parts of some text by using regular expressions because you want to validate that such input should be in some specific format or so. That's using regular expression match groups, but I believe you're doing right and any other approach would be an overkill.
I think for this case, split is the best.
But in case you are exploring other options also ,You can use regex to split something like this :
string[] lines = Regex.Split(master, "(\\.)");

C# string parsing question

I'm trying to split a string:
string f = r.ReadToEnd();
string[] seperators = new string[] {"[==========]"};
string[] result;
result = f.Split(seperators, StringSplitOptions.None);
There's this ========== thing that separates entries. For the life of me, I can't get this to work. I've got a ruby version working...BUT using the string splitter classes I thought I knew for .NET doesn't seem to be working so well.
Any ideas what I'm doing wrong?
You said that the separator is ========== but you're using [==========]. Try this:
string f = r.ReadToEnd();
string[] seperators = new string[] {"=========="};
string[] result;
result = f.Split(seperators, StringSplitOptions.None);
When I ran your code with the following modification:
string f = "string1[==========]string2[==========]string3";
string[] seperators = new string[] { "[==========]" };
string[] result;
result = f.Split(seperators, StringSplitOptions.None);
foreach (string x in result) Console.WriteLine(x);
The function writes out the strings as expected. I'd look at the contents of your file more closely - perhaps there is something in the encoding, or some other character that is missing when you devise a separator to work in C#/Windows.

Categories

Resources