Replace the particular part of string? [closed] - c#

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);

Related

Cleanest way to initialize string outside of conditional statement where it's assigned? [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 last month.
Improve this question
string str = ""
if(something) {
str = "a"
} else {
str = "b"
}
Is this the cleanest way? or is there a better way to expose the str outside of conditional scope without this dirty empty string initialization
you could set it using a ternary operator in a single line. Something like
string str = something ? "a" : "b";

how to remove last string element unnecessary text if exists? [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 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";

Strings with parameters for a method in 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 6 years ago.
Improve this question
My sentence is: !doar 12345, rayantt
Using string.Contains(!doar), I need get the other values as:
int mount = 12345;
string destiny = "rayannt";
Let the input be the input string as you stated in the question, searchString be the string that you wanted to search for; strParam and intParam are the two required outputs; Now consider the following code:
string input = "!doar 12345, rayantt";
string searchString = "!doar";
string strParam=string.Empty ;
int intParam=0;
if (input.Contains(searchString)) // check for the existence of the search string in given string
{
input = input.Replace(searchString, ""); // remove the searchstring from the input
string[] contents = input.Split(',');
int.TryParse(contents[0], out intParam); // collect the integer param
strParam=contents[1]; // collect the string param
}
// here you get 12345 in intParam and "rayantt" in strParam

how to use LINQ query? [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 7 years ago.
Improve this question
I have a input string,
string str1 = "aabcccabdfa";
I want to count the occurrence of each character and put it next to that character. So I need to output result as "a4b2c3d1f1". How can i achieve this with LINQ?
I try with "Dictionary" to do that, but not able to do that.
Thanks,
Do it like this:
string str1 = "aabcccabdfa";
string.Concat(str1.GroupBy(c => c).OrderBy(c => c.Key).Select(c => c.Key.ToString() + c.Count()));
string str1 = "aabcccabdfa";
var result=string.Concat(str1.GroupBy(a=>a)
.Select(a=>a.Key.ToString()+a.Count().ToString()));
string str1 = "aabcccabdfa";
var output = "";
str1.ToArray().Distinct().ToList().ForEach(x => output += x + (str1.Count(f => f == x).ToString()));

Trying to replace a string in the text file but not working [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 9 years ago.
Improve this question
class FunWithScheduling
{
static void Main()
{
StringBuilder newFile = new StringBuilder();
string temp = "";
string[] file = File.ReadAllLines(Scheduler.txt");
foreach (string line in file)
{
if (line.Contains("Subhadra"))
{
temp = line.Replace("Subhadra", "Thangam");
newFile.Append(temp + "\r\n");
continue;
}
newFile.Append(line + "\r\n");
}
File.WriteAllText("Scheduler.txt", newFile.ToString());
}
}
You are missing the opening quote in the following line:
string[] file = File.ReadAllLines(Scheduler.txt");
As such, the compiler is reading everything after that ending quote as the start of a string constant.
In order to fix this, you need to change the above line to the below, which has the opening quote:-
string[] file = File.ReadAllLines("Scheduler.txt");

Categories