Fulcrum
Fulcrum

Reputation: 47

Extract partial text from textbox

I have this text in textbox: mytextvariable.mytext

How to extract only text before the dot?

ex: textbox1 = mytext.text

Button Click

textbox2 = mytext without .text

Upvotes: 1

Views: 1970

Answers (4)

Joel
Joel

Reputation: 6243

You can use "Split" for this.

myText.Split('.').First();

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503290

Just use simple string manipulation:

string text = textBox1.Text;
int firstDot = text.IndexOf('.');
if (firstDot != -1)
{
    string firstPart = text.Susbtring(0, firstDot);
    textBox2.Text = firstPart;
}
else
{
    // Handle case with no dot
}

Fundamentally, this has nothing to do with the text coming from a textbox - it's just a string, really.

Upvotes: 6

Fernando
Fernando

Reputation: 1259

var beforeDot = new String(textBox.Text.TakeWhile(c => c != '.').ToArray());

Upvotes: 2

utopianheaven
utopianheaven

Reputation: 1277

if (mytextvariable.mytext.Contains("."))
  String stuffBeforeTheDot = mytextvariable.mytext.Substring(0, mytextvariable.mytext.IndexOf("."));

Upvotes: 1

Related Questions