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
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
Actually I want to split all words within the outer brackets ({}).
For example, if this is the string: "{{equal|even out|regularize} out|steady|tied|equal}", I want the split result array to be this:
{ equal,
even out,
regularize,
out,
steady,
tied,
equal }
string text = "try your {{equal|even out|regularize} out|steady|tied|equal} {champion|finest|top-quality}";
string1 = "try";
string2 = "your";
string3="{{equal|even out|regularize} out|steady|tied|equal}";
string4="{champion|finest|top-quality}";
How to split like this... please help me... Thanks !
try this :
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
text = text.Remove(0, text.IndexOf('{'));
var array = text.Split('{', '}', '|');
array.ToList().ForEach(item =>
{
Console.WriteLine(item);
});
output :
equal
even out
regularize
out
steady
tied
equal
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 4 years ago.
Improve this question
I have this FINAL PAYMENT $25 string on a MVC c# application.
I want to split into FINAL PAYMENT and 25
I tried doing this
string s = "FINAL PAYMENT $25";
string[] str1 = s.Split('$');
//result: 25
How can I get the rest. Can anyone help?
Split method returns a string array, if you need both elements of this array, Try:
string s = "FINAL PAYMENT $25";
string[] resArray = s.Split('$');
var FPayment = resArray[0];
var second25= resArray[1];
You can use indexOf instead of a Split
string s = "FINAL PAYMENT $25";
int index = s.IndexOf("$");
String final_pay = s.Substring(index + 1);
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 guys im trying to workout C# code to extract the first two words from string. below is code im doing.
public static string GetDetailsAsString(string Details)
{
string Items = //how to get first 2 word from string???
if (Items == null || Items.Length == 0)
return string.Empty;
else
return Items;
}
Define "words", if you want to get the first two words that are separated by white-spaces you can use String.Split and Enumerable.Take:
string[] words = Details.Split();
var twoWords = words.Take(2);
If you want them as separate string:
string firstWords = twoWords.First();
string secondWord = twoWords.Last();
If you want the first two words as single string you can use String.Join:
string twoWordsTogether = string.Join(" ", twoWords);
Note that this simple approach will replace new-line/tab characters with empty spaces.
Assuming the words are separated by whitespaces:
var WordsArray=Details.Split();
string Items = WordsArray[0] + ' ' + WordsArray[1];
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 have a text file that the user upload to the application through OpenFileDialog then when the file open the program is reading it line by line using:
string[] data = File.ReadAllLines(file);
how do i make a dynamic editing for each line so that after the Nth number or letter put a space then display the result to the user without writing over the file,
for example:
if my file contain:
0000000000BADBAD
2323040007BADAAD
4234420087BADBAC
the Display should be
0000000000 BADBAD
2323040007 BADAAD
4234420087 BADBAC
Once you have read all the lines into string array, data is isolated from your file i.e. any modification in data won't be reflected into your file. You can manipulate it like any other object.
One of the way manipulating data and achieving your result would be:
foreach (string value in data)
{
Console.WriteLine(value.Insert(10, " "));
}
You can loop throught your lines and extract the data with Regex, example:
string str = "0000000000BADBAD";
var match = Regex.Match(str, #"(?<Number>\d+)(?<Text>.*)");
var number = match.Groups["Number"].Value;
var text = match.Groups["Text"].Value;
string result = String.Format("{0} {1}", number, text);
And write result on your output.
Another alternative is using Linq
string str = "0000000000BADBAD";
int n;
string number = String.Join("", str.TakeWhile(c => int.TryParse(c.ToString(), out n)));
string text = String.Join("", str.SkipWhile(c => int.TryParse(c.ToString(), out n)));
string result = String.Format("{0} {1}", number, text);
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 have a string and I want to delete everything before a phrase, and then delete everything after a different phrase. i.e.,
myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words"
So the new string would be "these words I want".
Use String.Substring and String.IndexOf, it has also an overload with a start index:
string myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words";
string result = myString;
int indexOfFirstPhrase = myString.IndexOf("FIRSTPHRASE");
if(indexOfFirstPhrase >= 0)
{
indexOfFirstPhrase += "FIRSTPHRASE".Length;
int indexOfSecondPhrase = myString.IndexOf("SECONDPHRASE", indexOfFirstPhrase);
if (indexOfSecondPhrase >= 0)
result = myString.Substring(indexOfFirstPhrase, indexOfSecondPhrase - indexOfFirstPhrase);
else
result = myString.Substring(indexOfFirstPhrase);
}
Demonstration
Something like this?
string theWordsIWant = Regex.Replace(myString, #"^.*?FIRSTPHRASE\s*(.*?)\s*SECONDPHRASE.*$", "$1");
Demonstration
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
What i'm trying to do is delete certain text from a .text file. For example:
I have a .text file with the following text.
Hello
This
Is <----- I would like to delete this line from the file.
My
Text
I have tried to use the following code:
private void DeleteButton2_Click(object sender, EventArgs e)
{
if (comboBox2.SelectedItem == "")
{
MessageBox.Show("Please Select a Contact.");
}
else
{
comboBox2.Items.Remove(comboBox2.SelectedItem);
comboBox1.Items.Remove(comboBox2.SelectedItem);
File.Delete(comboBox2.SelectedItem + ".txt");
string SelectedItem = comboBox2.SelectedItem.ToString();
string empty = "";
string Readcurrentcontacts = File.ReadAllText(contactpath);
Readcurrentcontacts.Replace(SelectedItem, empty);
}
}
With no succesful results. If you need any further information please let me know! Thank you in advance!
The File.ReadLines and File.WriteAllLines methods would be useful here:
string SelectedItem = comboBox2.SelectedItem.ToString();
var allLines = File.ReadLines(contactpath)
// Linq filter to exclude selected item
var newLines = allLines.Where(line => line != SelectedItem);
File.WriteAllLines(contactpath, newLines);
Note that Where is a Linq extension method that takes an IEnumerable as input, and returns a subset based on the predicate that you provide. So, the line above takes the input (all lines in the file), and returns all lines that are not equal to SelectedItem.