Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Arrow Extracting a substring from 1st, 3rd, 5th places in textbox

    Hello coding forums, I am having difficulty in figuring out how to extract only certain character, or digits inside a textbox. I have been looking up similar methods but none seem to work for what I want to be done.

    I have a textbox called textBox1. I then made a string called
    Code:
    string total = textBox1.Text
    I tried to code a statement which grabs only the 1st, 3rd, 5th, etc of the string but it hasn't worked for me
    I've been trying:
    Code:
    for (int i = 0; i < total.Length; i++)
    	{
             textBox1.Text = total ;
    	 }
    PLEASE help me in figuring this out, and I'll instantly thank you cuz I really need to get this app done tonight before I lose my train of thought in the morning.

    sorry mods, you may delete this if you would like. I figured it out using: http://www.dotnetperls.com/substring

    sorry for the thread
    Last edited by crank01; 02-11-2012 at 05:22 AM.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,901
    Thanks
    46
    Thanked 202 Times in 201 Posts
    I think you should look at the string class; it would save you a lot of trouble ref
    also worth noting in things I've helped you with before you sent me a visual studio .sln file.... this means you have a VERY powerful tool at your disposal; click on an object (for instance "string") not what you named it, but the actual object class you are substantiating... hit F12. It will take you to the object browser and you can look at pretty much all the info you need WRT that class... also PLEASE peruse the msdn library... that link is a sub-link of the msdn library and it has example code for almost every class- if you ever have problems finding it in google usually the object + class + C# will give you the first hit as the msdn library
    eg
    Code:
    string class C#
    first hit gave msdn 1.1 framework (drop down switches to 4 or w/e)
    second link is .net 4 framework
    that library is VERY useful!!!!!!! help yourself and get comfy in there!

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    crank01 (02-11-2012)

  • #3
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by alykins View Post
    I think you should look at the string class; it would save you a lot of trouble ref
    also worth noting in things I've helped you with before you sent me a visual studio .sln file.... this means you have a VERY powerful tool at your disposal; click on an object (for instance "string") not what you named it, but the actual object class you are substantiating... hit F12. It will take you to the object browser and you can look at pretty much all the info you need WRT that class... also PLEASE peruse the msdn library... that link is a sub-link of the msdn library and it has example code for almost every class- if you ever have problems finding it in google usually the object + class + C# will give you the first hit as the msdn library
    eg
    Code:
    string class C#
    first hit gave msdn 1.1 framework (drop down switches to 4 or w/e)
    second link is .net 4 framework
    that library is VERY useful!!!!!!! help yourself and get comfy in there!
    cool I never knew about the f12 shortcut thx, and I'll take a look at the msdn library some more. I've been using it alot to solve many problems I have.

    now I already solved what I was trying to do, but out of curiosity, how would have you done it? My way was probably the quick n dirty method, but if you know a better way plz tell.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,901
    Thanks
    46
    Thanked 202 Times in 201 Posts
    I'm not really all to sure what exactly you were trying to do- what if what was in those "sub string" sections was not what you were expecting?
    eg
    expecting 123alykins456
    so sub-strings go to
    ~123
    ~alykins
    ~456
    but what if it got
    1aly234ki5n6s
    now the sub-strings are
    ~1al
    ~y234ki5
    ~n6s
    now what?

    I do not know what means you are trying to accomplish, if validating you can make an expression and validate it that way, if you are trying to see if a string contains something based on a set of "constraints" you could do something like...
    Code:
    private string[] constraints = new string[3] {"123", "alykins", "456"};
    private string order = null;
    private bool[] validatedStrings = new bool[3] {false, false, false};
    private bool StringEquals = false;
    
    public void validateString(string text){
    for(int i=0; i<constraints.length; i++){
    validatedStrings[i] = text.Contains(constraints[i]);
    }
    foreach(string s in constraints){
    order+=s;
    }
    StringEquals = (String.Compare(order,text,true))? true : false;
    }
    but again i have no clue what end you are trying to achieve would alter how I would do it etc...

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    well given a string of digits from a textBox, I converted the text-->toString, and then using
    Code:
    string sub = input.Substring(0, 1);
    I was able to take Only the first digit from the string, then using
    Code:
    string sub = input.Substring(0, 1) + input.Substring(2, 1);
    I was able to take the 3rd digit/character of the string and turn it into the string called sub.

    This way of coding is possibly not looked well upon, but who knows. It worked for me


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •