Removing the commas from string in c# [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 2 years ago.
Improve this question
I have this string:
AppleBanana
With C# code, I was managed to bring like this:
Apple,Banana,
But I don't want the last comma in it.
So for that I used:
string obj = myVar.Trim().Trim(','); // this is just to remove the whitespace and trailing comma
But nothing happens. My string is still the same i.e. it shows Apple,Banana,.
Then I tried this:
if (myVar.EndsWith(", "))
myVar= myVar.Remove(myVar.Length - 1); // but this removes all the commas from the string.
Where am I missing?

string s = "Apple,Banana,";
s = s.Remove(s.Length - 1);

It's clear that you're working with comma-delimited data, so treat it as such. Split it into an array. While you're splitting it, you can remove empty entries, which will have the effect of ignoring the final comma.
var list = myVar.Split(',', StringSplitOptions.IgnoreEmptyEntries);
If you need it as a comma-separated string again, join it:
var myVar = string.Join(",", list);

Remove the last character.
string obj = myVar.Remove(myVar.Length - 1);
Update
#JohnWu solution is better, more coherent, more robust.

Related

Spliting string into array based on curly brackets [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 1 year ago.
Improve this question
For Example I have String like this:
""dear customer{Customer name} your reference number is {referenceNumber}"
I want to get array=["{Customer name}",{referenceNumber}]"
I have to split based on curly bracket inside bracket value is changeable means it can be different for different cases I just need to split and get array of value inside brackets including brackets.
If you think about it, splitting on { and } will produce an array where every odd index is what you want..
.Split('{','}').Where((s,i)=>i%2==1).Select(s=>'{' + s + '}').ToArray();
Split the string, use the LINQ Where function that passes the int index to the predicate, insist that the index be odd (mod2 is 1) and select a new string that puts the brackets back on, ToArray

How to remove some characters or words from the middle of a string 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 3 years ago.
Improve this question
I am using .NET 2.1, i need to remove some extra parts of a string, but the issue is that these parts do not occur in the beginning nor in the end. they come in between. for instance we have:-
Ali and Ahmed are very very good friends.
but i want
Ali and Ahmed are very good friends.
I know the occurrence of the problem. Is there a way i can remove the word 'very' from the string.
You can use source.Replace("very",string.Empty);
String.Replace(oldString,newString) with the second parameter setted to string.Empty is the same as remove from the source string all "oldString".
You can try regular expressions:
To remove consequent duplicates of "very":
using System.Text.RegularExpressions;
...
string source = "Ali and Ahmed are very very good friends.";
string result = Regex.Replace(source, #"(very)(\s+\1)+", match => match.Groups[1].Value);
To remove consequent duplicates of any words:
string source = "Ali and Ahmed are are are very very good friends.";
string result = Regex.Replace(source, #"(\w+)(\s+\1)+", match => match.Groups[1].Value);
In both cases, the result is
Ali and Ahmed are very good friends.
If you know the starting index and the end index of the part you want to remove you can use this methode
public static string Delete(string str, int start, int end)
{
return str.Substring(0, start)
+ str.Substring(end, str.Length-end);
}
here is end the exclusive index of the part that is to deleted
e.g.
Delete("0123456789",3,5) -> "01256789"
Try it online!

Get String Between First n Character and Last n Charcter 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 4 years ago.
Improve this question
My String: draw.text("hello",11,22);
Need Output: 11
So i need to get a String Between first , character and Last , character.
Its not duplicate question.I not found same questions.
I need to get the 2nd argument in my string,the string between first , and last , in string
Split your string on the , character, remove the first and last item, and join the remaining components back together:
var input = "draw.text(\"hello\",11,22);";
var components = input.Split(',');
var result = String.Join(",", components.Skip(1).Take(components.Length - 2));
// yields 11
Note: using System.Linq is required for Skip and Take.

How can I split the code in csharp [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 6 years ago.
Improve this question
I want to store some string code in SQL like 120.002.123 or EXP.120.555. How can I split those strings to increase only the rightmost integer part? I don't want to change other parts of the string.
Ex. 120.002.124 or EXP.120.556
How can I do this in C#?
This might do the trick for you
string str = "EXP.120.556"; //120.002.123
str = String.Join(
".",
str.Split('.')
.Take(
str.Split('.').Length - 1
)
)
+ "." +
(
Convert.ToInt32
(
str.Split('.').LastOrDefault()
) + 1
).ToString();
So here in the code the first String.Join will join the other part of the string with . except of the last part which you want to increament using Take. Then Convert.ToInt32 will convert the last part of the string to a integer using LastOrDefault and then we add 1 to it and again convert it back to string using ToString
What you can do is to use String.Split method and split by . to get each individual number.
string test = "120.002.123";
string[] allparts = test.Split('.');
Then take the last entry and convert it to an integer
int lastNumber = Convert.ToInt32(allparts.Last());
Now you can do what ever you want with that integer.
With a little more research effort you could have solved it on your own.
Like this + this

String contains a lot of copies, how to get rid of them? [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 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

Categories