Ian Lundberg
Ian Lundberg

Reputation: 1883

how to increase the value of an integer in a string

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 [email protected] when i press the button to increase the 1 to a 2 it just does this instead. [email protected] and so on. how do i get it to just add 1 to the 1 and not put a 2 next to the 1?

Upvotes: 0

Views: 1346

Answers (5)

Brad Rem
Brad Rem

Reputation: 6026

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:

[email protected] 
[email protected] 
[email protected] 
etc.

Upvotes: 1

tobias86
tobias86

Reputation: 5029

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

e.g.

string originalEmail = "[email protected]";

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

txtEmail.Text = string.Format("{0}{1}@{2}", parts[0], i++, parts[1]);

Upvotes: 0

Antonio Bakula
Antonio Bakula

Reputation: 20693

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("[email protected]"); // => returns [email protected]
IncNumberBeforeAt("[email protected]"); // => returns [email protected]
IncNumberBeforeAt("email.com"); // => returns email.com
IncNumberBeforeAt("[email protected]"); // => returns [email protected]

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

This should work:

String email = "[email protected]";
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.

Upvotes: 0

Jon
Jon

Reputation: 40032

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;

Upvotes: 0

Related Questions