Substring when found character in a string in vba - excel

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.

Related

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

how to find string in another string by using vba only

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.

Compare two strings and extract common word out

In MATLAB how can we compare 2 strings and print the common word out. For Example string1 = "hello my name is bob"; and string2 = "today bob went to the park"; the word bob is common in both. What is the structure to follow.
Use intersect with strsplit for a one-liner -
common_word = intersect(strsplit(string1),strsplit(string2))
strsplit splits each string to cells of words and then intersect finds the common one out.
If you would like to avoid strsplit, you can use regexp instead -
common_word =intersect(regexp(string1,'\s','Split'),regexp(string2,'\s','Split'))
Bonus: Removing stop-words from the common words
Let's add some stop-words that are common to these two strings -
string1 = 'hello my name is bob and I am going to the zoo'
string2 = 'today bob went to the park'
Using the solution presented earlier, you would get -
common_word =
'bob' 'the' 'to'
Now, these words - 'the' and 'to' are part of the stop-words. If you would like to have them removed, let me suggest this - Removing stop words from single string
and it's accepted solution.
The final output would be 'bob', whom you were looking for!
If you are looking for matching words only, that are separated by spaces, you can use strsplit to change each string into cell arrays of words, then loop through and search for each one.
str1 = 'test if this works';
str2 = 'does this work?';
cell1 = strsplit(str1);
cell2 = strsplit(str2);
for n = 1:length(cell1)
for m = 1:length(cell2)
if strcmp(cell1{n},cell2{m})
disp(cell1{n});
end
end
end
Notice that in my example the last member of cell2 is 'work?' so if you have punctuation in your strings, you'll have to do a check for that (isletter might help).

Excel VBA extract words from string

I have a string in this format:
>word1>longword2>longerword3>word4>anotherword5>
In VBA I am looking to extract all words between the characters >. Any guidelines will be much appreciated - words contain numbers and special characters and are not fixed in length.
Use the split function : http://msdn.microsoft.com/library/6x627e5f(v=vs.90).aspx
There is an example at the end of the link.
You need to use something like:
Dim TestArray() As String;
TestArray = Split(myString, ">")
this code will solve your problem
Dim longString as String
longString = "word1>longword2>longerword3>word4>anotherword5>"
Dim arrayString() as String
arrayString = Split(longString, ">")

How to check if a word contains only upper-case characters in a string with vb6

I'd like to find out if any word in a string contains only upper-case characters with using vb6..
Lets say ive a string like this: "If you only knew the power of the DARKSIDE!"
Here i wanna catch "DARKSIDE" regardless of punctuation marks.
So how can i achive that? This should be easy.. Though i couldn't figure with a blink of an eye..
Dim astrSplitItems() As String
astrSplitItems = Split(strInputString, " ")
For intX = 0 To UBound(astrSplitItems)
If astrSplitItems(intX) = UCase(astrSplitItems(intX))
//Found
End If
Next
Try this:
If Chr("YOUR LETTER") = UCase(Chr("YOUR LETTER"))
If it's true the Letter is UPPERCASE
With a regular expression perhaps?
vb6 regex

Resources