?
Solved

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

Posted on 2015-01-14
8
Medium Priority
?
528 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 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

770 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