|
-
Sep 2nd, 2012, 01:19 PM
#1
Thread Starter
Lively Member
[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
-
Sep 2nd, 2012, 01:34 PM
#2
Re: Checking for last character in last appended string
To check the last character of a string
Code:
LastCharacter=TheString.SubString(TheString.Length,1)
-
Sep 2nd, 2012, 04:28 PM
#3
Thread Starter
Lively Member
Re: Checking for last character in last appended string
 Originally Posted by DataMiser
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
-
Sep 2nd, 2012, 04:50 PM
#4
Thread Starter
Lively Member
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.
-
Sep 3rd, 2012, 12:46 AM
#5
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by DataMiser
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:
Dim s As String = "testing#"
Dim c As Char = s(s.Length - 1)
Console.WriteLine(c)
Last edited by AceInfinity; Sep 3rd, 2012 at 12:52 AM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 3rd, 2012, 01:38 AM
#6
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.
-
Sep 3rd, 2012, 03:11 AM
#7
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by kevininstructor
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 3rd, 2012, 10:51 AM
#8
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by AceInfinity
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.
-
Sep 3rd, 2012, 11:23 AM
#9
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by kevininstructor
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...
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 4th, 2012, 09:39 AM
#10
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by AceInfinity
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.
-
Sep 4th, 2012, 10:47 PM
#11
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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 5th, 2012, 08:42 AM
#12
Re: [RESOLVED] Checking for last character in last appended string
 Originally Posted by AceInfinity
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.
-
Sep 5th, 2012, 06:31 PM
#13
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.
<<<------------
.NET Programming (2012 - 2018)
®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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|