how to remove last string element unnecessary text if exists? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string array with 3 elements like below for example.
string[] stringarray1;
stringarray1 = new string[5]{ “Element 1\n”, “Element 2\n”, “Element 3\n”, “Element 4\n”, “Element 5\nblablablabla” };
Here i need to check last element in string array having unnecessary dynamic text "\nblablablabla", if exists i need to remove(till last of the dynamic text) and replace with "Element 5\n" in last element.
How can I do this?

try this:
for (int i = 0; i < stringarray1.Length; i++)
{
stringarray1[i] = stringarray1[i].Split("\n")[0] + "\n";
}
You can split string with respect to "\n" and take first of it and add "\n" again. This will remove unnessesary characters from your string that i understand.

Instead of checking and replacing unnecessary text, you can replace what ever string you have with expected string
var lastIndex = stringarray1.Length -1;
stringarray1[lastIndex] = $"Element {lastIndex}\n";

Related

What is better for detection comments, split or substring [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I try to detect in C# Comments after ';'
Is it better to use Indexof and Substring or contains and split?
If i use index i get the ';' including all comments behind.
If i use contains and split i only get one comment but its less codelines.
indexComment = 0;
indexComment = line.IndexOf(";");
if (indexComment >= 0)
{
commands = line.Substring(0, indexComment);
comment = line.Substring(indexComment);
}else
{commands = line;}
VS
if (line.Contains(";"))
{
commands = line.Split(';')[0];
comment = line.Split(';')[1];
}else
{commands = line;}
Both codes works as expected but what would you prefer?
This is example code i want to detect
do something ;this line do something
x+5
;the line above add 5 to x
The split seems better because builds the substrings for you.
Just add the Count parameter with 2, so if you have more than one ';', they will all be included in your comment variable.
And use a temporary variable to avoid splitting twice.
if (line.Contains(";"))
{
string[] splitLine = line.Split(new char[] { ';' }, 2);
commands = splitLine[0];
comment = splitLine[1];
}
else
{ commands = line; }

I need to ignore a specific line while reading a file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to ignore reading the particular line while reading the whole document.
for example, I have chunk of data and I have read it using File.ReadAllText(filePath); and I need to ignore reading a particular line, Say 50 and need to read the other lines. So far I have the below code.
string fileName = "TextFile.config";
string filePath = Path.GetDirectoryName("TextFile.config") + fileName;
string text = File.ReadAllText(filePath);
You can use ReadLines and Where like here:
int[] ignoreLines = { 50 };
IEnumerable<string> relevantLines = File.ReadLines(filePath)
.Where((line, index) => !ignoreLines.Contains(index + 1));
string resultString = string.Join(Environment.NewLine, relevantLines);
Use File.ReadAllLines, this will give you all lines of the file in an array, you can then loop through this array to check for the line you want to ignore (or not ignore), (either with an index or with string.StartsWith / string.EndsWith
File.ReadLines Method (String)
Reads the lines of a file.
List.RemoveAt Method (Int32)
Removes the element at the specified index of the List.
List.RemoveRange Method (Int32, Int32)
Removes a range of elements from the List.
Exmaple
string fileName = "TextFile.config";
string filePath = Path.GetDirectoryName("TextFile.config") + fileName;
var lines = File.ReadLines(filePath).ToList();
lines.RemoveAt(49) // Remove 50th line
// or
lines.RemoveRange(49,10) // Remove 50th line + 9 more

assign indexes of 2 strings in loop C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to assign all indexes from one string to all indexes of another string.
I think its the best to make a for loop.
string stdalph = "apple";
string ourkey = "cream";
StringBuilder sbalph = new StringBuilder(stdalph);
StringBuilder sbkey = new StringBuilder(ourkey);
So like index of 'a' = index of 'c'
sbalph[0] = sbkey[0];
sbalph[1] = sbkey[1];
and so on
Would appreciate any help
// edit
ye but whats actually I need to perform is
string stdalph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456‌​789";
string ourkey = "xMK6JDC18hLoYeEkBSlIyVO0niadRf9qH5N4tbWpZ3wgAuc7GQjXm2FUvTz‌​Prs";
StringBuilder sbalph = new StringBuilder(stdalph);
StringBuilder sbkey = new StringBuilder(ourkey);
textBox2.Text = textBox1.Text;
and for example I write 'abc' and it translates it to 'xMK'
Yes, a for-loop you would normally use:
for (int i = 0; i < Math.Min(sbalph.Length, sbkey.Length); i++)
{
sbalph[i] = sbkey[i];
}
But the requirement isn't very clear. It seems you are assigning the second StringBuilder to the first StringBuilder, so why not simply:
sbalph = new StringBuilder(sbkey.ToString());

Split a string into two parts by using word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to split directory into two parts:
For example
//Hello//Products//App//Images//Room//40//Tulips.jpg
into
//Hello//Products//App
and
//Images//Room..40//Tulips.jpg
var splitOn = "App";
var path = "//Hello//Products//App//Images//Room//40//Tulips.jpg";
var parts = path.Split(new string[] { splitOn }, StringSplitOptions.None);
Console.WriteLine(parts[0] + splitOn);
Console.WriteLine(parts[1]);
In order to split by a word (or in this case folder) you need to wrap the term in a string array before passing it to the String.Split function. Splitting on "App" will also remove "App" from the result, so we concatenate it again before we write it to the console.
First split the string based on the double forward slash and then assign to array.
string path= Hello//Products//App//Images//Room//40//Tulips.jpg
string[] names = path.Split('//');
After this collect the words like this:-
string first_part=names[0] + "//" + names[1];

Replace the particular part of string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to replace particular string in another string if it contain that word . example
give string is "asp,mvc,c#,wpf" and another string is "<b>asp</b>,<b>wpf</b>" and my final result should be "<b>asp</b>,mvc,c#,<b>wpf</b>" , i have no idea about how to do it in c# code .please help me.
You can do this:
var str = "asp,mvc,c#,wpf";
var anotherStr = "<b>asp</b>,<b>wpf</b>";
var myArr = anotherStr.Replace("<b>", "").Replace("</b>", "").Split(',');
foreach (string value in myArr)
{
str = str.Replace(value, "<b>" + value + "</b>");
}
Console.WriteLine(str);

Categories