C# - Prepend A Substring If The Source String Does Not Begin With The Same Substring?
Oct 10, 2011
I'm trying to create a valid RegEx replacement pattern for correctly formatting specific .XML file names. Specifically, if a file name does not begin with a prefix, let's say, ACE_. If the XML file name and extension does not begin with ACE_, prepend the string ACE_ to the file name.For example, if my input source string is the following:
Widgets.xml
I would like to execute a single RegEx Replacement that would result in the string being:
ACE_Widgets.xml.
Conversely, if the string already begins with ACE_, I would like it to remain unchanged. Also,how can I include the pattern ".xml" to ensure that the string pattern for the file name and extension ends with ".xml" in the same matching pattern for the RegEx Replacement pattern? As for the match, I have some luck with the following:
^ACE_{1}[dD]+
Which indicates there is a match for the pattern if the input string is ACE_Widgets.xml and no match if the string is Widgets.xml
The RegEx pattern would suffice, but if you need to know the language in which I'd like to use the replacement pattern is in .NET 4.0 in either C# or VB.NET.
The following posting is close to what Im looking for, but with the inclusion of the *ix directory path prefix, and the use of preg_replace() in PHP, I'm having a bit of a struggle getting it to work with what I need to do:
Regular Expression: How to replace a string that does NOT start with something?
How will I select a substring with 2 criteria? I have a Richtextbox where I select a number after a : with this code; For Each line As String In Richtextbox1.Lines Dim equalsPos As Integer = line.IndexOf(":") + 1 txtFound.AppendText(line.Substring(equalsPos, line.Length - equalsPos)) Next This is working ok, but the whole line.length is appended for number2. The line ex. Some text:my number some text-my number2. I want my number appended to txtFound, and my number2 appended to txtFound2. With my code only number2 can be appended correct.
This is a tricky situation. I want to extract a certain substring from the strings given below, using VB 6.0. I process each string (row) in a for loop one by one.
from the Help in VS 2008: Replace: Returns a string in which a specified substring has been replaced with another substring a specified number of times.
Expression Required. String expression containing substring to replace. Find Required. Substring being searched for. Replacement Required. Replacement substring. Start
Optional. Position within Expression where substring search is to begin. If omitted, 1 is assumed.Count Optional. Number of substring substitutions to perform. If omitted, the default value is 1, which means "make all possible substitutions."
Compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings for values.
I wan't a method with which to find a string within another string. It should return the position of the first occurrence of the substring. (In VB 2008 !)
i am retrieving a iMAGE Extension variable string. for example: test.case-function.two.jpg and want to return the end '.JPG' portion only. so i can add to another variable value. Note the .JPG substring could be other Extension types such as PJPEG ect
Is there something I am missing to pre-check a string for a certain number of instances of a substring? Example.
"I am a test string, I will be searching this string later. I will be checking for every instance of the letter "i" and return the number of instances of that letter."
If I wanted to return an int for how many times i occurs in that string.
Basically I want to retrieve all possible substring matches with n characters from a string, Here's my initial code but it only returns 2 matches.[code]...
I've got some code that creates a list of AD groups that the user is a member of, with the intention of saying 'if user is a member of GroupX then allow admin access, if not allow basic access'.
I was using a StringCollection to store this list of Groups, and intended to use the Contains method to test for membership of my admin group, but the problem is that this method only compares the full string - but my AD groups values are formatted as cn=GroupX, etc....
I want to be easily able to determine if a particular substring (i.e. 'GroupX') appears in the list of groups. I could always iterate through the groups check each for a substring representing my AD group name, but I'm more interested in finding out if there is a 'better' way.
Clearly there are a number of repositories for the list of Groups, and it appears that Generics (List(Of String)) are more commonly preferred (which I may well implement anyway) but there is no in-built means of checking for a substring using this method either.
I've settled on using a List(Of), and I've borrowed from Dan's code to iterate through the list.
How do I make a regular expression to find a substring within a string that looks like: |sdrt446-7fdfs23| ? The number of characters and types change. So I need whatever in within the | |.
I need to create a code that searches an array of movie titles. When the user enters a specific string, the first movie title that contains that string is displayed. For example, entering "he" would display Sherlock Holmes as opposed to the Blind Side. I don't know how to create a code that reads a string that the user enters.[code]...
I'm creating a file thats need to be in this format url... I need to be able to set the limit of the string length for certain fields.Is there an easy way to set the limit of the field so that if the string was larger then the limit then just take the substring and if smaller would add extra spaces?I was able to accomplish something similar to this with integers by just using .toString("00000").
I'm writing some code that takes a report from the mainframe and converts it to a spreadsheet. They can't edit the code on the MF to give me a delimited file, so I'm stuck dealing with it as fixed width. It's working okay now, but I need to get it more stable before I release it for testing. My problem is that in any given line of data, say it could have three columns of numbers, each five chars wide at positions 10, 16, and 22. If on this one particular row, there's no data for the last two cols, it won't be padded with spaces; rather, the length of the string will be only 14. So, I can't just blindly have
I am trying to remove VB6 type code and use VB.Net code, in particular, remove "Left" and use "Substring". This code does not do what the old style code does. It seams to ignore the first two chars and forces the msgbox prompt no matter what I type in the textbox (at "Leave").
Private Sub TextBox5_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox5.Leave If TextBox5.Text = "" Then 'Do nothing
End WhileI am trying to read from the ": " to the end of the line. I keep getting this error: Index and length must refer to a location within the string. Parameter name: length
I'm by no means an experienced programmer. This task I have is very likely a one-off, so don't feel bad for giving me answers instead of pointing me in the right direction I've searched as long as I can bear, and just can't find what I'm after.I just need to be able to move to the next substring of a string.In this instance, "go to next substring" equates to "go to next line".I just need to be told that one command and I'll be on my way, but I can't find any trace of it. Here's a snippet of code with the magic command installed:
Dim count As Integer Dim line As String Dim lines As String() = My.Computer.FileSystem.ReadAllText("C: est.txt").Split(New Char() {vbCrLf}) For Each line In Lines If line.Contains("#") count = 0
Not sure if too many people know this, but the following line will cause an error:
GroupName.Substring(0, 3) = "jt_"
....if the length of GroupName is less than 3 characters. I always thought it would simply return whatever characters in GroupName, but no, it errors. I must be thinking of the old VB6 days.So, I now have to change the code to:
If (GroupName.Length > 2) Then If (GroupName.Substring(0, 3) = "jt_") Then
Note that the two comparisons need to be on separate lines. If they are on the same line, such as:
If (GroupName.Length > 2) and (GroupName.Substring(0, 3) = "jt_") Then then the code will still fail as the length command is executed at the same time as the substring command- which will cause the error when the GroupName length is less than 3.Just thought that those of us not aware of this should be!
I'm getting an ArgumentOutOfRange error when using substring function in .NET. I'm new to .NET so probably doing something wrong. I have a txtField, which is a text field component in GUI. I'm using Microsoft Visual Basic 2010 Express
I have a piece of code that I am using a for next statement and stepping through a string a little bit at a time. The problem is I get an error towards the end of my loop because my substring goes beyond the bounds of my string.
Dim Right As Integer = StartNumber.Value Dim left As Integer = LengthNumber.Value Dim i As Integer = 0
Which one would you prefer to extract a sub-string from the given string and why?I am thinking that since Left and Right are VB functions and not .NET functions, they may cause problems in the future in terms of compatibility.
This question was resolved in C#, and I don't know how to do it in VB net. I want to search through a directory of files and see if my string is contained within any of the filenames. Then I just need a bolean answer if it exists or not. I know how to use the file.exists...this is not what I need. The link below is to the C# response. How to check if filename contains substring in C#
My problem is at SubString will do the job for me, but next string could look like this: 101OldGranny91222 So I can't determind the number - for the split - of char in the string, because it changes depending on there name etc.
I have a text label, split half-and-half with a "/".label1.text = "I like dogs/I like cats"This will not be the same text (and not the same textlength)every times. I need to delete everything before the "/" with only a button. (for example, delete "I like dogs")