How to use String.Replace - c#

Quick question:
I have this String m_Author, m_Editor But I have some weird ID stuff within the string so if I do a WriteLine it will look like:
'16;#Luca Hostettler'
I know I can do the following:
string author = m_Author.Replace("16;#", "");
string editor = m_Editor.Replace("16;#", "");
And after that I will just have the name,
But I think in future I will have other people and other ID's.
So the question: Can I tell the String.Replace("#AndEverythingBeforeThat", "")
So i could also have
'14;#Luca Hostettler'
'15;#Hans Meier'
And would get the Output: Luca Hostettler, Hans Meier, without changing the code manually to m_Editor.Replace("14;#", ""), m_Editor.Replace("15;#", "")...?

It sounds like you want a regex of "at least one digit, then semi-colon and hash", with an anchor for "only at the start of the string":
string author = Regex.Replace(m_Author, #"^\d+;#", "");
Or to make it more reusable:
private static readonly Regex IdentifierMatcher = new Regex(#"^\d+;#");
...
string author = IdentifierMatcher.Replace(m_Author, "");
string editor = IdentifierMatcher.Repalce(m_Editor, "");
Note that there may be different appropriate solutions if:
The ID can be non-numeric
There may be other ignorable parts and you only want the value after the last hash

You could use regex or (what i'd prefer) IndexOf + Substring:
int indexOfHash = m_Author.IndexOf("#");
if(indexOfHash >= 0)
{
string author = m_Author.Substring(indexOfHash + 1);
}

or just,
var author = m_Author.Split('#').Last();

You can Split you string with # using string.Split() function this will give you two strings first everything before # and second everything after #

use String.Format
int number=5;
string userId = String.Format("{0};#",number)
string author = m_Author.Replace(userId, "");

If all you want to do is filter out everything that is not a letter or space, try:
var originalName = "#123;Firstname Lastname";
var filteredName = new string(originalName
.Where(c => Char.IsLetter(c) ||
Char.IsWhiteSpace(c))
.ToArray());
The example will produce Firstname Lastname

List<char> originalName = "15;#Hans Meier".ToList();
string newString = string.Concat(originalName.Where(x => originalName.IndexOf(x) > originalName.IndexOf('#')).ToList());

Related

How can I eliminate a quote from the start of my string using regex?

I have strings that sometimes start like this:
"[1][v5r,vi][uk]
Other times like this:
[1][v5r,vi][uk]
How can I remove the " when it appears at the start of a string using Regex? I know I need to do something like this, but not sure how to set it up:
regex = new Regex(#"(\n )?\[ant=[^\]]*\]");
regex.Replace(item.JmdictMeaning, ""));
If the string always starts with [1]:
int indexOfFirstElement = item.IndexOf("[1]");
if (indexOfFirstElement > 0)
item = item.Substring(indexOfFirstElement);
If you just want to start at the first [:
int indexOfFirstElement = item.IndexOf('[');
if (indexOfFirstElement > 0)
item = item.Substring(indexOfFirstElement);
Simpler than Regex, which is probably overkill for this problem.
Here you go
string input =#" ""[1][v5r,vi][uk]";
string pattern = #"^\s*""?|""?\s*$";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, "");
Console.WriteLine(result);
You can find my Example here in dotnetfiddle
string.StartsWith will do the trick
string str = "\"[1][v5r,vi][uk]";
if(str.StartsWith('"'))
str = str.Substring(1);
It can be done using indexOf and Substring
string str = "\"a[1][v5r,vi][uk]";
Console.WriteLine(str.Substring(str.IndexOf('[')));
use TrimStart() to remove this character if exists
string str = "\"a[1][v5r,vi][uk]";
str= str.TrimStart('\"');

Delete first element of a string using a method,C#

Im am very new in C#.
How i can delete first element of a string using a method,i find something on this site but didnt work so help me please.
For example
string newString = oldString.Substring(1);
If you want to remove the first word from your string you could use LINQ Skip combined with String.Split and String.Join:
string str = "How are you?";
string result = string.Join(" ", str.Split().Skip(1));//"are you?"
If you want only to remove the first letter you could use String.Substring:
string result = str.Substring(1);//"ow are you?";
Or if you want a LINQ solution you could use LINQ Skip:
string result = new string(str.Skip(1).ToArray());//"ow are you?";
Another solution is to use Remove method:
string myStr = "dsafavveebvesf";
//remove one character at position 0 - at the beginning
myStr = myStr.Remove(0, 1);
If you are not aware of Linq, you can simply use for loop to do the same.
Here you can use Split method of string.
string amit = "my name is amit";
string restultStr = string.Empty;
//taking all words in sentence in one array
string [] strWords = amit.Split();
//as we start this with 1 instead of 0, it will ignore first word
for (int i = 1; i < strWords.Length; i++)
{
restultStr += strWords[i] + " ";
}
EDIT
Now I see there are two onions here, removing first letter of the string and removing first word.
Above answer was to remove first word. if you want to just remove first letter, you can always do as suggested.
string amit = "my name is amit";
string restultStr = amit.Substring(1);

Substring IndexOf in c#

I have a string that looks like this: "texthere^D123456_02". But I want my result to be D123456.
this is what i do so far:
if (name.Contains("_"))
{
name = name.Substring(0, name.LastIndexOf('_'));
}
With this I remove at least the _02, however if I try the same way for ^ then I always get back texthere, even when I use name.IndexOf("^")
I also tried only to check for ^, to get at least the result:D123456_02 but still the same result.
I even tried to name.Replace("^" and then use the substring way I used before. But again the result stays the same.
texthere is not always the same length, so .Remove() is out of the question.
What am I doing wrong?
Thanks
When call Substring you should not start from 0, but from the index found:
String name = "texthere^D123456_02";
int indexTo = name.LastIndexOf('_');
if (indexTo < 0)
indexTo = name.Length;
int indexFrom = name.LastIndexOf('^', indexTo - 1);
if (indexFrom >= 0)
name = name.Substring(indexFrom + 1, indexTo - indexFrom - 1);
string s = "texthere^D123456_02";
string result= s.Substring(s.IndexOf("^") + 1);//Remove all before
result = result.Remove(result.IndexOf("_"));//Remove all after
Use the String.Split method :
var split1 = name.Split('^')[1];
var yourText = split1.Split('_')[0];
Or you could use RegExp to achieve basically the same.
Your easiest solution would be to split the string first, and then use your original solution for the second part.
string name = "texthere^D123456_02";
string secondPart = name.Split('^')[1]; // This will be D123456_02
Afterwards you can use the Substring as before.
With Regular Expression
string s = "texthere^D123456_02";
Regex r1 = new Regex(#"\^(.*)_");
MatchCollection mc = r1.Matches(s);
Console.WriteLine("Result is " + mc[0].Groups[1].Value);
An alternative to what's already been suggested is to use regex:
string result = Regex.Match("texthere^D123456_02", #"\^(.*)_").Groups[1].Value; // D123456
use regex.
Regex regex = new Regex(#"\^(.*)_");
Match match = regex.Match(name);
if(match.Success)
{
name= match.Groups[1].Value
}
An easier way would be to use Split
string s = "texthere^D123456_02";
string[] a = s.Split('^', '_');
if (a.Length == 3) // correct
{
}
Well, if you use the same code you posted, it's doing the right thing, you start to retrieve characters from the char 0 and stop when it finds "^", so what you will get is "texthere".
If you want only the value, then use this:
name = name.Substring(0, name.LastIndexOf('_')).Substring(name.IndexOf("^") + 1);
It will first remove whatever is after the "_" and whatever is before "^".
Substring takes a position and a length, so you need to actually figure out where your caret position is and where the underscore is to calculate the length
var name = "texthere^D123456_02";
if(name.Contains('_'))
{
var caretPos = name.IndexOf('^') + 1; // skip ahead
var underscorePos = name.IndexOf('_');
var length = underscorePos - caretPos;
var substring = name.Substring(caretPos, length);
Debug.Print(substring);
};
Try this and let me know how it goes
string inputtext = "texthere^D123456_02";
string pattern = #".+\^([A-Z]+[0-9]+)\_[0-9]+";
string result = Regex.Match(inputtext, pattern).Groups[1].Value;
String name = "texthere^D123456_02"
print name.split('_', '^')[1]
This splits your string at all occurrences of _ and ^ and returns the list of strings after the split. Since the string you need D123456 would be at the 1st index, (i.e. the 2nd position), I have printed out that.
If you are just wanting the "d123456" it's simple with just String.Split() there is no need for anything else. Just define the index you want afterwards. There are overloads on Split() for this very reason.
//...
var source = "texthere^D123456_02";
var result = source.Split(new char[] {'^', '_'}, StringSplitOptions.RemoveEmptyEntries)[1];
Console.WriteLine(result);
//Outputs: "D123456"
Hope this helps.

best possible way to get given substring

lets say I have string in format as below:
[val1].[val2].[val3] ...
What is the best way to get the value from the last bracket set [valx] ?
so for given example
[val1].[val2].[val3]
the result would be val3
You have to define best first, best in terms of readability or cpu-cycles?
I assume this is efficient and readable enough:
string values = "[val1].[val2].[val3]";
string lastValue = values.Split('.').Last().Trim('[',']');
or with Substring which can be more efficient, but it's not as safe since you have to handle the case that's there no dot at all.
lastValue = values.Substring(values.LastIndexOf('.') + 1).Trim('[',']');
So you need to check this first:
int indexOflastDot = values.LastIndexOf('.');
if(indexOflastDot >= 0)
{
lastValue = values.Substring(indexOflastDot + 1).Trim('[',']');
}
For a quick solution to your problem (so not structural),
I'd say:
var startIndex = input.LastIndexOf(".["); // getting the last
then using the Substring method
var value = input.Substring(startIndex + 2, input.Length - (startIndex - 2)); // 2 comes from the length of ".[".
then removing the "]" with TrimEnd function
var value = value.TrimEnd(']');
But this is by all means not the only solution, and not structural to apply.. Just one of many answers to your problem.
I think you want to access the valx.
The easiest solution that comes in my mind is this one:
public void Test()
{
var splitted = "[val1].[val2].[val3]".Split('.');
var val3 = splitted[2];
}
You can use following:
string[] myStrings = ("[val1].[val2].[val3]").Split('.');
Now you can access via index. For last you can use myStrings[myStrings.length - 1]
Providing, that none of val1...valN contains '.', '[' or ']' you can use a simple Linq code:
String str = #"[val1].[val2].[val3]";
String[] vals = str.Split('.').Select((x) => x.TrimStart('[').TrimEnd(']')).ToArray();
Or if all you want is the last value:
String str = #"[val1].[val2].[val3]";
String last = str.Split('.').Last().TrimStart('[').TrimEnd(']');
I'm assuming you always need the last brace. I would do it like this:
string input = "[val1].[val2].[val3]";
string[] splittedInput = input.split('.');
string lastBraceSet = splittedInput[splittedInput.length-1];
string result = lastBraceSet.Substring(1, lastBraceSet.Length - 2);
string str = "[val1].[val2].[val3]";
string last = str.Split('.').LastOrDefault();
string result = last.Replace("[", "").Replace("]", "");
string input="[val1].[val2].[val3]";
int startpoint=input.LastIndexOf("[")+1;
string result=input.Substring(startpoint,input.Length-startpoint-1);
I'd use the below regex. One warning is that it won't work if there are unbalanced square brackets after the last pair of brackets. Most of the answers given suffer from that though.
string s = "[val1].[val2].[val3]"
string pattern = #"(?<=\[)[^\]]+(?=\][^\[\]]*$)"
Match m = Regex.Match(s, pattern)
string result;
if (m.Success)
{
result = m.Value;
}
I would use regular expression, as they are the most clear from intention point of view:
string input = "[val1].[val2].[val3] ...";
string match = Regex.Matches(input, #"\[val\d+\]")
.Cast<Match>()
.Select(m => m.Value)
.Last();

C# using regexp while working with strings

I have a string in this format "string1;string2;string3;...;stringn"
; as a delimiter
I need to delete some string value of which I know, say valueForDelete
I use string.Split(';') method to find my value, delete it and then create a new string without deleted value.
I'm wondering is it possible to make this process easy with regex?
var values = "string1;string2;string3;string4";
var cleanedValues = String.Join(";",
values.Split(';')
.Where(x => x != "string3")
.ToArray())
Regex is a useful tool, and could be used for this, but often hard to maintain. Something like the above would likely provide an easier to maintain solution. Regex can be tricky if your string also contain regex characters. As a bonus, this is easy to extend.
static string CleanItUp(string values, params string[] removeMe)
{
return String.Join(";",
values.Split(';')
.Except(removeMe)
.ToArray());
}
Used like.
var retString = CleanItUp("string1;string2;string3;", "string1", "string2");
// returns "string3"
Why not just:
string s = "string1;string2;string3;valueForDelete;string4"
s = s.Replace("valueForDelete;", string.Empty).Replace("valueForDelete", string.Empty);
The second replace is for if the value is the last one.
However possible with RegEx, using Split and Join will be your easiest, most functional choice. If you had a more complex method of choosing what Strings to delete, you could use the Where clause.
String input = "string1;string2;string3";
String valueForDelete = "string2";
String[] parts = input.Split(';');
var allowed = parts.Where(str => !str.Equals(valueForDelete));
String output = String.Join(";", allowed);
If you are simply removing an exact value than String.Replace would be better.
Use this Regex to find valueForDelete: (?<=;|^)valueForDelete(?=;|$)
const string Pattern = #"(?<=;|^)string3(?=;|$)";
var s = "string1;string2;string3;string4;string5;";
var res = Regex.Replace(s, Pattern, string.Empty);
Regex will do that but not sure that for what you are asking it would be faster. .Split is fast. If you were spitting on something more complex then you would have to use regex. I assume you are using StringBuilder to build the new string? String += is slow. When you new the StringBuilder make it the size you expect.
For Replacement ensuring no other data is affected (using LINQ):
string test = "string1;string2;string3;valueForDelete;stringn";
test = String.Join(";", test.Split(';').Where(s => s != "valueForDelete"));
For simple replacement (using String.Replace()):
string test = "string1;string2;string3;valueForDelete;stringn";
test = test.Replace("valueForDelete;", "");
Couldn't you just say
var myString = "string1;string2;string3;string4;string5;";
myString = myString.Replace("string3;", "");
The result would be a myString with the value "string1;string2;string4;string5;"
EDIT: Created as a regex
public static Regex regex = new Regex("(?:^|;)string3(;|$)",
RegexOptions.CultureInvariant | RegexOptions.Compiled
);
myString = regex.Replace(myString, ";");
...only flaw I see at the moment is if myString = "string3"; it results in myString = ";";
Why Can't you just do this?
public static string Replace(string input)
{
input = input.Replace("valueToDelete;", "");
return input ;
}

Categories

Resources