1

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;#", "")...?

2
  • Will there always be 1 and only 1 '#'? Commented Dec 11, 2014 at 14:04
  • No, cause it will raise to 20++ if you have more users ;) but the # will always be there Commented Dec 11, 2014 at 14:06

7 Answers 7

9

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
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe anchored to the beginning of the string?
@JoeWhite: Yup, was just fixing that as well as adding another example :)
Hi @Jon, yes you're right, this is exactly the method I searched for, already tested and it worked right away :)
4

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

6 Comments

Note that this would also replace the start of "Don'tReplaceThis#foo". I guess it depends on whether there's always an ID.
@JonSkeet: in my understanding OP wants only the part after the hash.
It's always there^^ so why you'd prefer that if you even have more code @TimSchmelter?
Well we know that's the case if it's an ID before the hash, but we don't know whether sometimes there may not be an ID at the start of the string, but there's still a hash somewhere. It's entirely possible that this would work, of course. It can also be simplified to just string author = m_Author.Substring(m_Author.IndexOf('#') + 1); as that will still give the whole string if there's no hash. (And I believe it's optimized appropriately.)
@Unlockedluca: because it is more readable and more efficient. But as Jon has mentioned you can use a one-liner if it's always there: m_Author.Substring(m_Author.IndexOf('#') + 1). I just wanted to show the proper use of the return value of IndexOf.
|
1

or just,

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

4 Comments

This assumes there's no other hash in the string. What about #14;#Stuff#Other stuff?
I am assuming that only the portion after the last '#' is required.
Yes, and I'm saying that I don't see anything in the question to justify that assumption. Of course it may be valid. It's hard to tell. Although in this case I'd use LastIndexOf and Substring probably.
The question is ambiguous and I agree a Regex is more valid for the general case and possibly faster.
1

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

Comments

0

use String.Format

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

2 Comments

That assumes you know what the number is - and in that case, it would be easier to just use concatenation: m_Author.Replace(number + ";#", "")
oh,yes,then use regex)as other answers says)
0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.