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.
Related
Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)
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
In my case a string contains for example something like 2500.00. Also you input a string in the same format for example 250.0 which would be converted to 250.00. These strings will be converted to float and they will be added or subtracted.
Now I want to check if the string contains two "." somewhere for example 2.50.00 or 250..00. In that case an errormessage should be displayed.
Therefore my question is how am I able to check if a string contains two "." characters at any position of the string?
You may check if a dot appears more than once in a string with a simple method checking if the first index of the char is not equal to the index of the last char occurrence:
boolean containsTwoDots(String str) {
return str.indexOf('.') != str.lastIndexOf('.');
}
str1="D:\\User\\Desktop\\result.jpg";
str2="D:\\User\\Desktop\\";
I want to get the difference between str1 and str2, which is "result.jpg" so that i could use the string "result.jpg" for something else.
Any solution?
I would use string replace and find:
std::string string = std::regex_replace(string1, std::regex("\\" + str2), "");
It is only pseudocode.
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.