Solved

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

Posted on 2015-01-14
8
309 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

19 Experts available now in Live!

Get 1:1 Help Now