how to find string in another string by using vba only - string

So I have
Dim str1
str1 = "Cat"
Dim str2
str2 = "concatenate"
I wanted to know is there a way I can match str1 with str2 and return positive non-zero number if there is match (case-insensitive) for str1 in str2 ?

For VBA, The instr is the way to go:
InStr(1, str2, str1, vbTextCompare)
The first part is where it starts looking, in this case the first character.
The second is the string in which the search is happening.
The third is the search parameter.
The fourth determines whether it looks at the text of the binary value. So for a case-insensitive we use vbtextcompare.

Use the indexOf method:
str2.indexOf(str1)

Use instr or instrrev:
instr(str2, str1, 1)
See msdn for more info.
This does exactly what you need.

Related

Substring when found character in a string in vba

I have folder path is string same as below:
"D:\aaaa\bbbb\cccc\eeee\ffff\abcd.txt"
Then i would like to get 2 string as below:
Str1 = "bbbb"
Str2 = "ffff"
My idea is that I would be like this. I would first find the position of "" then substring by index. but I don't know how to implement it.
So anyone can you help me this problem in vba.

Remove Trailing Zeros from a Hexadecimal string

I have a column of Hexadecimal strings with many TRAILING zeros.
The problem i have is that the trailing Zeros from the string, needs to be removed
I have searched for a VBA formula such as Trim but my solution has not worked.
Is there a VBA formula I can use to remove all these Trailing zeros from each of the strings.
An example of the HEX string is 4153523132633403277E7F0000000000000000000000000000. I would like to have it in a format of 4153523132633403277E7F
The big issue is that the Hexadecimal strings can be of various lengths.
Formula:
You could try:
Formula in B1:
=LET(a,TEXTSPLIT(A1,,"0"),TEXTJOIN("0",0,TAKE(a,XMATCH("?*",a,2,-1))))
This would TEXTSPLIT() the input and the fact that we can then use XMATCH() to return the position of the last non-empty string with a wildcard match ?*. However, given the fact we can use arrays in our TEXTSPLIT() function, a little less verbose could be:
=TEXTBEFORE(A1,TAKE(TEXTSPLIT(A1,TEXTSPLIT(A1,"0",,1)),,-1),-1)
Or another option, though more verbose, is to use REDUCE() for what it's intended to do, which is to loop a given array:
=REDUCE(A1,SEQUENCE(LEN(A1)),LAMBDA(a,b,IF(RIGHT(a)="0",LEFT(a,LEN(a)-1),a)))
VBA:
If VBA is a must, one way of dealing with this is through the RTrim() function. Since your HEX-string should not contain spaces to begin with I think the following is a safe bet:
Sub Test()
Dim s As String: s = "4153523132633403277E7F0000000000000000000000000000"
Dim s_new As String
s_new = Replace(RTrim(Replace(s, "0", " ")), " ", "0")
Debug.Print s_new
End Sub
If you happen to have spaces anywhere else in your string, another option would be to look for trailing zero's using a regular expression:
Sub Test()
Dim s As String: s = "4153523132633403277E7F0000000000000000000000000000"
Dim s_new As String
With CreateObject("vbscript.regexp")
.Pattern = "0+$"
s_new = .Replace(s, "")
End With
Debug.Print s_new
End Sub
Both the above options should print: 4153523132633403277E7F
As far as I know, there is no function to do that for you. The way I would do it is presented in the pseudo-code below:
while last character is "0"
remove last character
end while
It is quit slow, but VBA itself is not race car either, so you will probably not notice especially if you do not need to that for many times at once.
A more beautiful solution would involve VBA being able to search for the beginning or the end of a string.
An improvement of the solution above is to parse the string backwards and count the "0" characters, and then remove them all at the same time.

Remove characters A-Z from string [duplicate]

This question already has answers here:
Extracting digits from a cell with varying char length
(4 answers)
Closed 2 years ago.
I need to be able to remove all alphabetical characters from a string, leaving just the numbers behind.
I don't need to worry about any other characters like ,.?# and so on, just the letters of the alphabet a-z, regardless of case.
The closest I could get to a solution was the exact opposite, the below VBA is able to remove the numbers from a string.
Function removenumbers(ByVal input1 As String) As String
Dim x
Dim tmp As String
tmp = input1
For x = a To Z
tmp = Replace(tmp, x, "")
Next
removenumbers = tmp
End Function
Is there any modification I can make to remove the letters rather than numbers to the above, or am I going at this completely wrong.
The letters could fall anywhere in the string, and there is no pattern to the strings.
Failing this I will use CTRL + H to remove all letters one by one, but may need to repeat this again each week so UDF would be much quicker.
I'm using Office 365 on Excel 16
Option Explicit
dim mystring as String
dim regex as new RegExp
Private Function rgclean(ByVal mystring As String) As String
'function that find and replace string if contains regex pattern
'returns str
With regex
.Global = True ' return all matches found in string
.Pattern = "[A-Z]" ' add [A-Za-z] if you want lower case as well the regex pattern will pick all letters from A-Z and
End With
rgclean = regex.Replace(mystring, "") '.. and replaces everything else with ""
End Function
Try using regular expression.
Make sure you enable regular expression on: Tools > References > checkbox: "Microsoft VBScript Regular Expressions 5.5"
The function will remove anything from [A-Z], if you want to include lower case add [A-Za-z] into the regex.pattern values. ( .Pattern = "[A-Za-z]")
You just pass the string into the function, and the function will use regular expression to remove any words from in a string
Thanks

Matlab String Split when it is a String object

The other answers for similar question works if the string is str1 = 'MynameisJohn' within single quotes. For example, str1(1:2) gives 'My'.
But if the string is str1 = "MynameisJohn" with double quotes, the above usage str1(1:2) does not work and gives an out of bounds error. The size of str1 in this case is just a 1 by 1 matrix.
In the second case, how do I split the string to get the words in it, assuming there are no whitespaces (hence delimiters can't be used). We can assume the lenghts of my split are constant.
EDIT
I think I found the answer myself. str2 = char(str1) converts the string array str1 to a character array and then similar constructs str2(1:2) works.
Conversion to char and then indexing works as you have posted. If you would like the result to stay as a string another way to extract substring is to use extract functions. For example,
str1 = string('MynameisJohn');
substr = extractBefore(str1,3)
substr =
string
"My"
In this case substr is still a string type. Doc for extractBefore is at https://www.mathworks.com/help/matlab/ref/extractbefore.html

Comparing one string to another string using compareTo method

When comparing string to another string using compareTO() method, here we compare one string to another string right. My doubt is which one is argument string in this two
example : int result = str1.compareTo( str2 );
If you write
str1.compareTo(str2)
then you get back an integer that is
negative if str1 precedes str2,
zero if str1 equals str2, and
positive if str1 is comes after str2.
Here, str1 is the receiver object (since it comes before the dot) and str2 is the argument.

Resources