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,"."));
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Which is the best way I can compare two C# string variables containing comma separated values and find the difference?
The scenario is like.
string variable1 = "AAA, BBB, CCC, DDD";
string variable2 = "AAA, CCC, DDD, EEE";
And I need the result like "BBB" (the value which is present in variable2 but not in variable1.
Thanks
Use Except:-
var result = variable1.Split(new char[] {','})
.Except(variable2.Split(new char[] {','})).ToArray();
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, "");
}
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 regx pattern in C# which find substring in any string which comes in middle only. Let say ,
Input : "toprohitpop rohittoppop toppoprohit"
find substring : "rohit"
Replace with : "$$$$"
Output : "top$$$$pop rohittoppop toppoprohit"
if substring "rohit" comes in left or right of the string then it should not be replaced.Substring "rohit" will only be replaced when it comes in middle of string .
Thanks in advance.
Use non-word-break anchors:
\Brohit\B
The \B will only match if it is in the middle of a word.
Read about it.
var input = "toprohitpop rohittoppop toppoprohit";
var regex = new Regex(#"\Brohit\B");
var output = regex.Replace(input, "$$$$$$$$");
See "Anchors" in Regular Expression Language.
Also, be careful with the '$' in the substitution string (see comments)
Use the following regex: .+rohit.+
Basically it enforeces at least one char before rohit and one after
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
User input a word eg( Hello ), how do I put hello into an array? such that
array[1]=h
array[2]=e
array[3]=l
array[4]=l
array[5]=o
Use the string method ToCharArray like this :
char[] input = "hello".ToCharArray();
If you have a lot of logic to do after, I'd recommend using a List instead, which you can get with :
List<char> input = "hello".ToList();
And as a side note, h will be in yourArray[0] (or .ElementAt(0)) not [1], since C# is 0-based; all indexes start at 0 instead of 1.
Actually you don't need to do anything special to accomplish this. You can already access the characters in the string by using an indexer, like this:
"Hello"[0] will return "H", "Hello"[1] will return "e" and so on.
Try this:
char myArray[] = "Hello".ToCharArray();
Try below instead
char [] array = "Hello".ToArray();
use this
string[] a=Console.ReadLine() // Get String From user
char[] myCharArray= a.ToCharArray();
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();
}