Results 1 to 13 of 13

Thread: [RESOLVED] Checking for last character in last appended string

  1. #1
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Resolved [RESOLVED] Checking for last character in last appended string

    Hi guys,

    How do I check if the last character in a string that was appended to a long string is a "}" ?


    Code:
      Dim AnimMainStr As New System.Text.StringBuilder
    
    
    Dim firstItems  As String = "$('#" & triggerId & "').click(function(){" & vbCrLf & _
                                    vbTab & " $('#" & targetId & "').animate({ " 
    
            AnimMainStr.Append(firstItems.ToString)
    
            If IsWidth Then
                AnimMainStr.Append(vbCrLf & vbTab & vbTab & prpt3)
            End If
            If IsHeight Then
                AnimMainStr.Append("," & vbCrLf & vbTab & vbTab & prpt4)
            End If
    I basicaly need to prevent inserting a comma "," before a property if the property is the first one, and in case the user only selects one property.
    I know I have to use some kind of Regex , but i'm new to vb any ideias? Or is there another and more elegant solution than Regex.

    Thanks,
    Mike

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,945

    Re: Checking for last character in last appended string

    To check the last character of a string
    Code:
    LastCharacter=TheString.SubString(TheString.Length,1)

  3. #3
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Checking for last character in last appended string

    Quote Originally Posted by DataMiser View Post
    To check the last character of a string
    Code:
    LastCharacter=TheString.SubString(TheString.Length,1)
    Thanks for your reply. I also need to check for last appended string, and then it's last character.

    Thanks,
    Mike

  4. #4
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Checking for last character in last appended string

    Right.
    I found another way of sorting my dilema.

    Code:
    If IsHeight Then
    If AnimMainStr.ToString = firstItems Then
                AnimMainStr.Append( vbCrLf & vbTab & vbTab & prpt4)
    Else 
    AnimMainStr.Append( "," & vbCrLf & vbTab & vbTab & prpt4)
    
            End If
    But Many thanks for your reply, is always good to learn.

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    582

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by DataMiser View Post
    To check the last character of a string
    Code:
    LastCharacter=TheString.SubString(TheString.Length,1)
    TheString.Length would not be a valid index within the string...

    You shouldn't even have to use SubString:
    vbnet Code:
    1. Dim s As String = "testing#"
    2. Dim c As Char = s(s.Length - 1)
    3. Console.WriteLine(c)
    Last edited by AceInfinity; Sep 3rd, 2012 at 12:52 AM.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,001

    Re: [RESOLVED] Checking for last character in last appended string

    You could use String.Last

    Code:
    Dim Value As String = "123".AppendIfNeeded("}"c)
    Console.WriteLine(Value)
    Console.WriteLine(Value.AppendIfNeeded("}"c))
    Place the following into a code module
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function AppendIfNeeded(ByVal sender As String, ByVal Character As Char) As String
        Return IIf(sender.Last = Character, sender, String.Concat(sender, Character)).ToString
    End Function
    Of course you could use the single line in the extension method in your code too.

  7. #7
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    582

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by kevininstructor View Post
    You could use String.Last

    Code:
    Dim Value As String = "123".AppendIfNeeded("}"c)
    Console.WriteLine(Value)
    Console.WriteLine(Value.AppendIfNeeded("}"c))
    Place the following into a code module
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function AppendIfNeeded(ByVal sender As String, ByVal Character As Char) As String
        Return IIf(sender.Last = Character, sender, String.Concat(sender, Character)).ToString
    End Function
    Of course you could use the single line in the extension method in your code too.
    String.Last() is slower than Indexing.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  8. #8
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,001

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by AceInfinity View Post
    String.Last() is slower than Indexing.
    Perhaps it is, but in this case is it a factor that would slow down the current operation enough to be concerned to choice one method of the other?
    Personally I doubt it.

  9. #9
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    582

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by kevininstructor View Post
    Perhaps it is, but in this case is it a factor that would slow down the current operation enough to be concerned to choice one method of the other?
    Personally I doubt it.
    It's a personal preference to optimize code regardless for me. Maybe you're executing this more than once? Even if execution times are over a period of 10 minutes, if you call it 100 times, even if the difference is 50ms, you've wasted 5 seconds of time that didn't have to be wasted just sitting there waiting for the method to complete.

    If you're not in it to try and optimize your code then some would consider it laziness if you don't look for opportunities where things may be optimized in your code.

    It depends on how large the string is, but the bigger the string here, the more diverse the results are, making them more noticable. That's usually the case with most optimized to non-optimized code, which is why I try to optimize as much as possible in my code.

    Say you aren't in it to optimize anything because you have 3 or 4 methods that are slower than another way of doing something, but the change is "small" as you say? Combined, these slower methods can still add up as well to wasted time.

    Just my way of looking at it...
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  10. #10
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,001

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by AceInfinity View Post
    It's a personal preference to optimize code regardless for me. Maybe you're executing this more than once? Even if execution times are over a period of 10 minutes, if you call it 100 times, even if the difference is 50ms, you've wasted 5 seconds of time that didn't have to be wasted just sitting there waiting for the method to complete.

    If you're not in it to try and optimize your code then some would consider it laziness if you don't look for opportunities where things may be optimized in your code.

    It depends on how large the string is, but the bigger the string here, the more diverse the results are, making them more noticable. That's usually the case with most optimized to non-optimized code, which is why I try to optimize as much as possible in my code.

    Say you aren't in it to optimize anything because you have 3 or 4 methods that are slower than another way of doing something, but the change is "small" as you say? Combined, these slower methods can still add up as well to wasted time.

    Just my way of looking at it...
    I appreciate the fact for optimizing code but consider that even when you want too it is not always possible when you are up against a strict timeline. Good example, I am part of a team that works on Oregon Department of Revenue’s online electronic filing system which has a front facing interface and backend. For enhancements we generally have 6-8 months of time to code and test. Over the past 15 years we always have time to deliver an optimized solution. But there are times when the system which is 24-7 cannot be down long during business hours especially in these economic bad times we sometimes may not have code that is optimal i.e. a complete process might take two seconds longer because of this that might take for example another five minutes this is unacceptable to business so we roll with what we have. Now at the time of coding the optimal method is either known or unknown but with pressure to resume business you go with what you have. If the un-optimized gets the job done but is slow then a new task is added to optimize the code at non-peak hours. So what I am saying is this is not being lazy but working within constraints. Going back to the extension Last I did a test this morning on an application which reads in a file stream from a large PDF file to extrapolate information and place this information into a backend database. I knew of one section that uses the method you recommended and replaced it with the Last extension. Ran this against the job, results were extremely close, under 3 seconds so yes one is faster but not enough for me to consider myself lazy. The line which utilizes this is around 300 chars on average and was hit 4,671 times.

  11. #11
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    582

    Re: [RESOLVED] Checking for last character in last appended string

    I appreciate the fact for optimizing code but consider that even when you want too it is not always possible when you are up against a strict timeline.
    Touche' I can't disagree... I'm usually fast, but regardless of my speed I don't really have any strict timelines or "deadlines". Something i've forgotten about

    Good example, I am part of a team that works on Oregon Department of Revenue’s online electronic filing system which has a front facing interface and backend. For enhancements we generally have 6-8 months of time to code and test. Over the past 15 years we always have time to deliver an optimized solution. But there are times when the system which is 24-7 cannot be down long during business hours especially in these economic bad times we sometimes may not have code that is optimal i.e. a complete process might take two seconds longer because of this that might take for example another five minutes this is unacceptable to business so we roll with what we have. Now at the time of coding the optimal method is either known or unknown but with pressure to resume business you go with what you have. If the un-optimized gets the job done but is slow then a new task is added to optimize the code at non-peak hours. So what I am saying is this is not being lazy but working within constraints. Going back to the extension Last I did a test this morning on an application which reads in a file stream from a large PDF file to extrapolate information and place this information into a backend database. I knew of one section that uses the method you recommended and replaced it with the Last extension. Ran this against the job, results were extremely close, under 3 seconds so yes one is faster but not enough for me to consider myself lazy. The line which utilizes this is around 300 chars on average and was hit 4,671 times.
    Agree again, results are close enough that it doesn't really matter. I'm just a stubborn picky guy when it comes to programming

    Cheers friend!
    ~Ace
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  12. #12
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,001

    Re: [RESOLVED] Checking for last character in last appended string

    Quote Originally Posted by AceInfinity View Post
    Touche' I can't disagree... I'm usually fast, but regardless of my speed I don't really have any strict timelines or "deadlines". Something i've forgotten about



    Agree again, results are close enough that it doesn't really matter. I'm just a stubborn picky guy when it comes to programming

    Cheers friend!
    ~Ace
    I appriciate your thoughts and in this world of programming I think it pays to be stubborn otherwise those who have poor standards with power get away with murder in regards to not optimizing.

  13. #13
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 11
    Posts
    582

    Re: [RESOLVED] Checking for last character in last appended string

    @kevininstructor - That or a picky boss regardless of a strict deadline lol. Keeps all the procrastination away from assuming bad habits or even good code, but non-optimized, is always a good thing. I am just picky that's all. Nothing more to it than that. The way I see it, I try to optimize it to the best way I know. Perhaps there's a better way than what I know, but if I don't know about it, then there's no excuse from my end.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

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