我的一个朋友带着这种我无法解释的奇怪行为来找我,任何洞察力的观点都会受到赞赏.
我正在运行VS 2005(C#2.0),以下代码显示了该行为
int rr = "test".IndexOf("");
Console.WriteLine(rr.ToString());
Run Code Online (Sandbox Code Playgroud)
上面的代码,打印"0",清楚地表明它应该返回-1
这也发生在Java中,下面的Class显示了这种行为:
public class Test{
public static void main(String[] args){
System.out.println("Result->"+("test".indexOf("")));
}
}
Run Code Online (Sandbox Code Playgroud)
我正在运行Java 1.6.0_17
我有一个看起来像这样的字符串 -
"FirstName||Sam LastName||Jones Address||123 Main ST ..."(100多个不同的值)
我想从整个字符串中找到Sam和Jones.
所以firstname = originalstring.substring......等等
有谁知道我怎么做到这一点?
加法 - 我想我忘了提几件事.
FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....
Run Code Online (Sandbox Code Playgroud)
所以现在,如果我计算不会帮助我的字符数,原因可能需要更多的项目而不仅仅是名字和姓氏.
我有一个返回数组的for循环:return
1st loop:
arr[0]
arr[1]
arr[2]
arr[3]
Run Code Online (Sandbox Code Playgroud)
返回
2nd loop
arr[4]
arr[5]
arr[6]
arr[7]
arr[8]
Run Code Online (Sandbox Code Playgroud)
我得到的长度是9.返回
我想要的是索引的实际数量,即我需要它为5.我怎么能这样做...当有一种方法,当我每次进入每个循环时,它从0开始,以便我得到所有的适当长度循环?
当IndexOf用于查找后跟大值char的char(例如char 700为')时,IndexOf无法识别您正在查找的char.
例如
string find = "abc?abcabc";
int index = find.IndexOf("c");
Run Code Online (Sandbox Code Playgroud)
在此代码中,索引应为2,但它返回6.
有办法解决这个问题吗?
我在Lua中遇到问题,检查字符串值是否未在另一个字符串中显示.
这就是我在Javascript中可能会这样做的方式:
'my string'.indexOf('no-cache') === -1 // true
Run Code Online (Sandbox Code Playgroud)
但在Lua我试图使用string模块,这给了我意想不到的响应:
string.find('my string', 'no-cache') -- nil, that's fine but..
string.find('no-cache', 'no-cache') -- nil.. that's weird
string.find('no-cache', 'no') -- 1, 2 here it's right.. strange..
Run Code Online (Sandbox Code Playgroud) 以下代码没有给我正确的答案.
class Point {
int x; int y;
public Point(int a,int b){
this.x=a;this.y=b;
}
}
class A{
public static void main(String[] args){
ArrayList<Point> p=new ArrayList<Point>();
p.add(new Point(3,4));
p.add(new Point(1,2));
System.out.println(p.indexOf(1,2));
}
}
Run Code Online (Sandbox Code Playgroud)
这给了-1;
一般来说,如果给出了arraylist of point,我们怎样才能找到数组中特定点的索引?
我有一个简单的问题.如何在java中使indexof case不敏感.这个问题已经在一些论坛得到了回答,但我不明白答案.
比如说.我有一个字符串s = Phone(Conf)我想拉这样的记录(Conf),但是用户正在输入CONF或conf或Conf等.所以我的程序应该能够拉出记录在任何情况下它都会找到conf这个词.
if(s.indexOf("(")>-1&& s.indexOf("Conf")>-4 && s.lastIndexOf(")")>-1)
{
String s1=s.substring(s.indexOf("(Conf"),s.lastIndexOf(")")+1);
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?如果字符串仅为(Conf),则上面的代码将其拉出.
如何获取列表中一个实例的索引?
class Points {
int x, y;
Point(this.x, this.y);
}
void main() {
var pts = new List();
int Lx;
int Ly;
Points pt = new Points(25,55); // new instance
pts.add(pt);
int index = pts.indexOf(25); // Problem !!! How to obtain the index in a list of instances ?
if (index != -1 ){
Lx = lp1.elementAt(index).x;
Ly = lp1.elementAt(index).y;
print('X=$Lx Y=$Ly');
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将样式应用于用户选择的文本(鼠标拖动),我需要获取所选文本的开始和结束索引。
我试过使用“indexOf(...)”方法。但它返回所选子字符串的第一次出现。我想要子字符串相对于原始字符串的实际位置。例如..,如果我在位置 3 [ YOL O Cobe]处选择字母“O” ,我希望索引为 3,但 indexOf() 方法返回 1,这是 [Y O LO ] 中第一次出现的“O”科比]。
有没有其他方法可以获取所选文本的实际开始和结束索引而不是第一次出现?
function getSelectionText()
{
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
}
return text;
}
document.getElementById('ip').addEventListener('mouseup',function(e)
{
var txt=this.innerText;
console.log(txt);
var selectedText = getSelectionText();
console.log(selectedText);
var start = txt.indexOf(selectedText);
var end = start + selectedText.length;
if (start >= 0 && end >= 0){
console.log("start: " + start);
console.log("end: " + end);
}
});Run Code Online (Sandbox Code Playgroud)
<div id="ip">YOLO Cobe</div>Run Code Online (Sandbox Code Playgroud)
我正在从一个带有空格后的非组合变音符号的字符串中创建一个子字符串。这样做时,我检查字符串,.Contains()然后执行子字符串。当我char在 an 中使用空格时.IndexOf(),程序按预期执行,但是当使用字符串“”时,.IndexOf()程序中会引发异常。如下面的示例所示,只有string在主要重音变音符号 (U+02C8) 之前的 a 会抛出ArgumentOutOfRangeException.
简单代码(由 John 建议编辑):
string a = "a? pr??z?nt";
string b = "ma? ?pr?znt";
// A
Console.WriteLine(a.IndexOf(" ")); // string index: 2
Console.WriteLine(a.IndexOf(' ')); // char index: 2
// B
Console.WriteLine(b.IndexOf(" ")); // string index: -1
Console.WriteLine(b.IndexOf(' ')); // char index: 3
Run Code Online (Sandbox Code Playgroud)
我测试的示例代码:
const string iPresent = "a? pr??z?nt",
myPresent = "ma? ?pr?znt";
if(iPresent.Contains(' '))
{
Console.WriteLine(iPresent.Substring(0, iPresent.IndexOf(' ')));
} …Run Code Online (Sandbox Code Playgroud)