IndexOf bug when string contains 'AA' - string

I've run into a strange problem. I have a string with a value containing 'AA'.
I'm trying to find IndexOf the first accouring A. When I ask if the string Contains("A") it returns true. When using IndexOf("A") I keeps getting the default value -1! (se the picture below)
So far i tested there is only a problem with 'A' and 'a'.
When putting 3 a's in the string I get the index of number 3, as if the first two doesn't exsist.
When adding an extra a to the string, I get the default value -1 again.
I don't know what is causing this, I have a suspision that it's somehow connected to some langauge setting. I'm from denmark, and the use of the letters aa is a synonym for å.
Have anyone else experinced a simular problem or have a suggestion how to avoid it?
System information:
Windows 7 Ultimate (English)
Visual Studio 10 Premium

'aa' is handled as an entity if the culture is da-DK. The question is sort of a duplicate, see String StartsWith() issue with Danish text.

Hmmm I have tried the same now. It works...
static void XYZ()
{
string a = "aaa";
string b = "AAA";
if(a.Contains("a"))
{
Console.WriteLine(a.IndexOf("a"));
}
if(b.Contains("A"))
{
Console.WriteLine(b.IndexOf("A"));
}
}
But wouldn't it be the best to seach for a "aa" and "AA"? I can speak danish and I know that there are single a's too ;-)

Related

Can´t find the correct class for string methods in Godot

I am trying to make my program read and edit text. I was trying to use the Position=find ( String , 0 ) method but I get the error:
The method "find" isn't declared in the current class.
I have tried different classes but I cant find the correct one.
Which would that be?
How may I find the correct class in the future?
Thanks in advance.
The find is in the String class.
From the documentation:
int find ( String what, int from=0 )
Finds the first occurrence of a substring. Returns the starting position of the substring or -1 if not found.
So what happens?
When you do Position = find(String, 0) You are calling find from which ever class your code is inside, not from String.
Furthermore, I only see one String there… Which String do you want to find in which one? You need two. Yes, find only takes one String, but it is an instance method, not a static method. You are meant to call it on the one you want to search, like this:
var position := string_in_which_you_are_looking.find(string_you_are_looking_for, 0)

Unity: Comparing strings doesn't seem to work in this case?

I'm trying to compare to strings in a function to be able to do stuff if it's true. However, for some reason the two strings I'm comparing are never equal to eachother.
I am creating a new string in a function, getting data from a Json and then comparing that string with another string in another class. This doesn't work for some reason. The Debug shows that both strings are equal, but the if-statement returns false.
I created two other string variables (hello & hello2) with the same value ("Hello") and this time it's comparing correctly.
Check the images below. What am I doing wrong?
As you can see in the console, both strings have the same value:
Image 1. Here's where I create the string (zoneId).
Image 2. Further down in the same function, same for-loop.
Here's where I'm trying to compare the string created in this function with another string from another class.
Image 3. As you can see in the console it's looping through the jsonArray. But it's returning false even though it's clear that both strings have the same value.
Image 4 and 5. Here I am testing with two other strings inside the function and they are working fine.
Does it has something to do calling a string from another class?
Here's how my other string in userInfo is set up:
public string userID { get; private set; }
In Image 3, is there an additional space before the second 2?
How are you processing Main.Instance.userInfo.zoneID?
You need to use string.Equals for string comparison instead of operator == or !=.
Are string.Equals() and == operator really same?

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

visual studio 2013 - string must be exactly one character long

Visual Studio is giving me the following error when I submit and store in the database.
"string must be exactly one character long"
to try to resolve tried this but without success:
cmd.Parameters.Add("#nomeEmpresa", OleDb.OleDbType.Integer).Value = Convert.ToChar(cbxEmpresa.Text)
cmd.Parameters.Add("#nomeContacto", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtNomeContacto.Text)
cmd.Parameters.Add("#apelidoContacto", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtApelidoContacto.Text)
cmd.Parameters.Add("#funcao", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtFuncao.Text)
how can I solve this problem?
If you look at the documentation of Convert.ToChar you could read
Converts the first character of a specified string to a Unicode
character. Namespace: System Assembly: mscorlib (in mscorlib.dll)
Syntax
public static char ToChar( string value )
valueType: System.String
A string of length 1.
That's the reason of your error.
However your code seems to be incorrect. If you want to pass Integer values types by your user to your sql you need to convert your input using something like Int32.TryParse(textbox.text)
Instead if you want to pass string values you need to change your parameter type to SqlDbType.NVarChar.
Convert.ToChar(string) requires that the string only contain a single character. You need to gaurrantee this for each of the strings before you call it, or manually select the first character from the string, or something similar.
Docs for Convert.ToChar(string), throws FormatException "The length of value is not 1."
http://msdn.microsoft.com/en-us/library/5f3ew98y(v=vs.110).aspx

Object Initializer Formatting

Which setting(s) in Resharper 8 are reformatting my object initializer like this? (i.e. with the comma on it's own line).
var snowDepthProcessor = new DataProcessor<SnowDepthModel>
{
Name = "Snow Depth"
,
DataRetriever = snowReportRetriever
,
Parser = new SnowDepthParser()
,
...
};
I've tried every combination of settings I can find/think of; I do want the lines chopped but I don't want the comma to be on its own line.
I share your pain. I've to admit my solution is just partial because it needs change style from what you'd like to use to
List<string> demo = new List<string>
{
"a",
"b",
"c"
}
Then formatting is not changed. I'd also much rather use syntax you've described so I've created R# ticket https://youtrack.jetbrains.com/issue/RSRP-453704 but until it's resolved this is only way how suppress that (it behave in same way even in latest R#10)
EDIT: Good news. Seems to be fixed since R# 2017.1

Resources