0

Ok I currently have this code

        public int i = 0; //this is outside of the private void button1_click
        string str = txtEmail.Text;
        int pos = str.LastIndexOf("@");
        string str2 = str.Substring(pos);
        string str3 = str.Substring(0, str.LastIndexOf("@"));
        txtEmail.Text = str3 + i++ + str2;

it splits the email into 2 string then combines them with an integer between them, But I want it to change the integer. But that code just makes it lets saying the text becomes awesome1@email.com when i press the button to increase the 1 to a 2 it just does this instead. awesome12@email.com and so on. how do i get it to just add 1 to the 1 and not put a 2 next to the 1?

leppie
  • 115,091
  • 17
  • 196
  • 297
Ian Lundberg
  • 1,813
  • 10
  • 31
  • 48

5 Answers5

1

I tested the following and it looks like it solves your problem. Change this line of yours:

string str = txtEmail.Text; 

To this:

string str = txtEmail.Text.Replace(string.Format("{0}@", i - 1), "@");

It sets it up so that your email addresses will be in the form of:

awesome1@email.com 
awesome2@email.com 
awesome3@email.com 
etc.
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
0

Not sure where i is coming from in this code but hopefully this will work

    string str = txtEmail.Text;
    int pos = str.LastIndexOf("@");
    string str2 = str.Substring(pos);
    string str3 = str.Substring(0, str.LastIndexOf("@"));
    txtEmail.Text = str3 + (i++).ToSting() + str2;
Jon
  • 38,814
  • 81
  • 233
  • 382
  • doesnt work still just adds on to the string and makes it longer doesnt replace the number with the +1 version of itself – Ian Lundberg Mar 26 '12 at 09:25
  • Agreed although I dont think in this case it will be too much of a memory problem – Jon Mar 26 '12 at 09:31
  • Better to use string.Concat, avoid the GC overhead of making a stringbuilder – Cronan Mar 26 '12 at 09:38
  • Also, this might answer it: http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c – Cronan Mar 26 '12 at 09:38
  • @RickHoving i dont think string builder works that way? cause doing that gave me an error saying that stringbuilder "doesnt have a constructor that contains 3 arguments". – Ian Lundberg Mar 26 '12 at 09:39
  • @IanLundberg yeah I cinda messed up there, look at my answer for the correct usage. – Rick Hoving Mar 26 '12 at 09:42
0

This should work:

String email = "awesome1@email.com";
String[] tokens = email.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
const String allowed = "0123456789";
String part1 = "";
String numberStr = "";
foreach (char c in tokens[0].Reverse())
{
    if (allowed.Contains(c) && part1.Length==0)
    {
        numberStr += c;
    }
    else
    {
        part1 += c;
    }
}
part1 = new String(part1.Reverse().ToArray());
int number = int.Parse(new String(numberStr.Reverse().ToArray()));
String result = String.Format("{0}{1}@{2}", part1, number++, tokens[1]);

Although it looks a little bit cumbersome. Accept a Regex answer if there's one.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

This will work

public static string IncNumberBeforeAt(string text)
{
  int lastAt = text.LastIndexOf('@');
  if (lastAt != -1)
  {
    int pos = lastAt - 1;
    string num = "";
    while (text[pos] >= '0' && text[pos] <= '9')
    {
      num = text[pos] + num;
      pos--;
      if (pos < 0)
        break;          
    }
    int numInc = int.Parse(num) + 1;
    return text.Replace(num.ToString() + "@", numInc.ToString() + "@");
  }
  else
  {
    return text;
  }
}

test

IncNumberBeforeAt("awesome1@email.com"); // => returns awesome2@email.com
IncNumberBeforeAt("awesome234@email.com"); // => returns awesome235@email.com
IncNumberBeforeAt("email.com"); // => returns email.com
IncNumberBeforeAt("5@email.com"); // => returns 6@email.com
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
0

You will have to keep track of your original e-mail address:

e.g.

string originalEmail = "test@gmail.com";

var parts = originalEmail.Split('@');

txtEmail.Text = string.Format("{0}{1}@{2}", parts[0], i++, parts[1]);
tobias86
  • 4,979
  • 1
  • 21
  • 30