Solved

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

Posted on 2015-01-14
8
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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