Splitting a string into parts [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split a string in C# for example:
120530
so it will be like this:
Day: 12
Time: 0530
But it will be without spaces, just as it is 120530
How can I do that?

I'm assuming that you know that it will always be 6 digits with the first 2 being day and last 4 being time.
Utilize the Substring() method of your string object...
string allTogether = "120530";
string day = allTogether.Substring(0, 2);
string time = allTogether.Substring(2);

var dayAndTime = "120530";
var day = dayAndTime.Substring(0, 2);
var time = dayAndTime.Substring(2, 4);

Related

C# subtraction of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am sorry for that bad Title, but basicaly my problem is really simple. I got 1 string which is basic alphabet and the second string which is gonna be part of the alphabet (8 characters) which user will fill up by himself. If 2 characters are the same, they will get removed and then rest of characters will be in the TextBox3. could someone pls help me ?
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = TextBox2.Text;
I assume you want to check the existence of substring and remove from the parent string, Try this
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = textBox2.Text;
if (alphabet.ToLowerInvariant().Contains(special))
{
textBox3.Text = alphabet.Replace(special, "");
}

How to String Format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've an array of strings below and wanted to format like the followings, what is the best way of doing that? Thanks in advance.
line[0] = "This is line one two tree";
line[1] = "This is Abc Cde";
line[2] = "This is cjdj";
I want it to format to display like this
This is line one two tree
This is Abc Cde..........
This is cjdj.............
You can use the string.PadRight() method, coupled with determining which of the array of strings is the widest:
var width = line.Max(l => l.Length);
foreach (var l in line)
Console.WriteLine(l.PadRight(width, '.'));
You could use:
var output = string.Join(Environment.NewLine, line.Select(l => l.PadRight(line[0].Length, '-').ToArray());
Use string.PadRight to pad each string, up to the specified length, with instances of a specified character.
Use PadRight:
http://msdn.microsoft.com/en-us/library/system.string.padright.aspx
e.g.
int len = line[0].Length;
Console.WriteLine(line[0]);
Console.WriteLine(line[1].PadRight(len,"."));
Console.WriteLine(line[2].PadRight(len,"."));

How to change the decimal to 12 digit string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to change the double (currency) to 12 digit in c# toString();
For example
12.50 => 000000001250
Try this:
string value = ((decimal)(12.50 * 100)).ToString().PadLeft(12, '0');
int a = (int)(12.50 * 100);
Console.WriteLine((a).ToString("D12"));
Try this
string formattedValue = value.ToString("000000000000");
value being the input and formattedValue as the output.

Print Duplicate Items using LINQ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to print duplicate items using LINQ.
e.g. I want to print 1 at 10 times.
Here 1 is a string and 10 (Dynamic Number) is the number of times I want to print this string.
How can I do this?
You can use this constructor overload:
int count = 10;
string s = new String('1', count);
If you really wanted to use Linq, you could use Enumerable.Repeat:
int copies = 10;
foreach(var s in Enumerable.Repeat("1", copies))
{
Console.WriteLine(s);
}
But for that matter, a simple for-loop would work too:
int copies = 10;
for(int i = 0; i < copies; i++)
{
Console.WriteLine("1");
}

Trimming and Removing text from string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim a string and remove all the words that occur after a certain word.
For example - If the string contains 'very' text
string mySentence=" Today is very nice day! ";
if (mysentence.Contains(very))
{
//remove everything that starts with 'very' until rest of the line..
}
result should be:
Today is
First you split using the required word
string[] splits = mysentence.Split("very");
Since you've already made certain that "very" is inside the string, this will get you two strings. You want the first one (the split before the "very"). You need to trim the extra space from that one so:
string result = splits[0].Trim();
Try this
string mySentence = " Today is very nice day! ";
if (mySentence.Contains("very"))
{
mySentence = mySentence.Remove(mySentence.IndexOf("very")).Trim();
}

Categories