0

I know this should be simple yet I am a little stuck. I am reading in a text file line by line. Each line is formated the same based off an ICD. I need to take the data at a specific location and replace it with x's.

For Example:

Line = "First Name Last Name Street Address State ZIP Other Data"

This is a fixed length ICD so address always starts at lets say position 100 and goes through 150 I need to replace everything position 100 to 150 with x's.

From there I am writing the line out to a new file and that part is working fine.

Thank you so much for your help.

6
  • 9
    Okay, so you've told us what you're trying to do. Now, how far have you got, and where are you stuck? Commented Jul 31, 2013 at 20:19
  • This is where I am stuck. I am not sure if I use str.Substring(), or RegEx or Replace or something else entirely. Commented Jul 31, 2013 at 20:28
  • 1
    Sounds like Substring would be the best bet to me. You know the exact bits of the string you want - it's not like you need a pattern. Commented Jul 31, 2013 at 20:29
  • @user1128637 - Do you want to replace whitespace with x as well? Commented Jul 31, 2013 at 20:30
  • Ok, so if I use substring to grab the sections that are good. Then I could just put the substrings back together with a substring of x's for the section that I cut out. Commented Jul 31, 2013 at 20:32

3 Answers 3

2

Use this:

Dim newLine As String = Line.Substring(0, 100) & New String("x"c, 50) & line.Substring(150)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! That worked like a charm. I thought it might be something simple. It's been one of those days!
0

You can create a function that takes in the string, start index, and length and returns the string with the replaced characters. This will also handle error cases where the length is greater than string length (in which case the rest of the string is replaced with the char you've chosen).

Private Shared Function ReplaceCharsWithChar(input As String, firstIndex As Integer, length As Integer, replaceChar As Char) As String
    Dim sb As New StringBuilder(input)
    For i As Integer = firstIndex To Math.Min(firstIndex + length, input.Length) - 1
        sb(i) = replaceChar
    Next
    Return sb.ToString()
End Function

And call it like this

Dim input As String = "First Name Last Name Street Address State ZIP Other Data" 
Dim result As String = ReplaceCharsWithChar(input, 10, 5, "x"C)

'output would be First Namexxxxx Name Street Address State ZIP Other Data

Comments

0

There is no built-in method to do that, so you'll need to implement it yourself. The simplest way would be to use the String.Substring method to extract the parts you want (the beginning and ending of the string), and then concatenate them back together with the replacement value. For instance:

Dim newValue As String = line.Substring(0, 99) & New String("X"c, 50) & line.Substring(150)

However, if you need to replace more than one section of the string, it may be easier and more efficient to use the StringBuilder, which allows you to manipulate each character in place:

Dim builder As New StringBuilder(line)
For i As Integer = 100 to 149
    builder.Chars(i) = "X"c
Next
line = builder.ToString()

5 Comments

Might want to edit the top portion of code. The first argument to Substring is an index but the second argument is a length. Good point about the StringBuilder class and strings being immutable, by the way.
@DouglasBarbin Which part are you saying is wrong? I'm not seeing it.
Thank you! This is a great idea too. The single line of code works for what I need, but I can see where StringBuilder would come in handy if I needed to make more than one manipulation.
@StevenDoggart Looking at it closer, I think you're right. He wants to keep the first 99 characters, and X out characters # 100-150 (inclusive). For some reason I thought he wanted to keep the first 100 and X out # 101-150.
@DouglasBarbin Ah. Yeah, I think he was just using those numbers as ballpark examples, which is why he wasn't very specific. I tried to stay as close to his numbers as I could, but it required some imagination :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.