Solved

VB.NEt - Find Position of the First Character in a String

Posted on 2015-01-14
8
327 Views
Last Modified: 2015-01-21
Good Day Experts!

I have another inquiry that I cannot seem to figure out how to achieve.

I have a string of data that I need to find the position of the first character other than the leading spaces.  This is so I can determine when the line pattern changes.

     12345Order
     5678Order
  5678Total

Is it possible to get the position of first character in a string other than spaces?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
8 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40549258
Len(myString)-Len(LTrim(mystring))+1
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40549389
See "Regular Expression Language - Quick Reference":
http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Use the "\S" pattern (case-sensitive!) to match the first non-white-space character.

Example:
Dim data As String = "     5678Order"
Dim index As Integer = System.Text.RegularExpressions.Regex.Match(data, "\S").Index ' Find first non-white-space character

Open in new window


*You could alternatively use "\d" to find the first decimal digit.
0
 

Expert Comment

by:Paul Weegar
ID: 40549762
IS there a need for the leading space(s)?  If you don't need them, use the trim function (which removes leading and trailing spaces). Then use the Instr function to locate whatever you need to find.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40549997
All valid responses, although I can't help mentioning my favorite quote about RegEx.  "When you have a problem, use regular expressions, and now you have two problems".  A simple trim should be sufficient to remove leading and trailing spaces.  However if you are needing to trim things and seek for line pattern changes to convert text to valid data maybe your parsing strategy needs to be changed.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Jimbo99999
ID: 40551362
Hello everyone. Yes the leading spaces are required as I am using it to trigger when to stop processing.  As I process the Order lines(which can vary in number 1 or 2 or 10 etc), I am wanting to look for the leading spaces.  Then as soon as the amount of leading spaces change then I know to stop processing.

     12345Order
     5678Order
  5678Total
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 40551424
You could also explicitly check for a space at the beginning using StartsWith(), then Trim() it afterwards to get rid of the spaces:
        If line.StartsWith(" ") Then
            line = line.Trim
            ' ... now do something with "line" ...
        Else
            ' ... do something else ...
        End If

Open in new window

0
 

Author Comment

by:Jimbo99999
ID: 40556682
I am still working on this over the weekend trying all comments.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40562018
Thanks everyone...it is working.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now