barriers / 阅读 / 详情

C#中怎么简单的判断TextBox输入的是否为数字???

2023-07-11 02:04:44
共9条回复
西柚不是西游
* 回复内容中包含的链接未经审核,可能存在风险,暂不予完整展示!
这个不能简单判断。
比如:输入数字后可以删除,按方向键删除等。有很多情况。
我也试过了,自己写了一个控件,不是很理想,不过能用。
在winForm中可以直接使用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace DataManageModule.Common
{
/// <summary>
/// 只能输入数字的文本框:TextBox
/// 说明:该文本框除了0~9、一个小数据点,上下左右键、删除键(Delete)、BackSpace键 能输入外其它的都不能输入
/// </summary>
public class NumberTextBox : TextBox
{
private ToolTip toolTip;//当输入非数字字符时,弹出的汽泡提示框
private System.ComponentModel.IContainer components;//系统本身
protected bool isAllowKeyPress;//是否允许按銉

private double maxValue = double.MaxValue;
[Description("输入数字的最大值")]
public double MaxValue
{
get { return maxValue; }
set { maxValue = value; }
}
private double minValue = double.MinValue;
[Description("输入数字的最小值")]
public double MinValue
{
get { return minValue; }
set { minValue = value; }
}

private bool isNegativeEnable = true;
/// <summary>
/// 是否允许为负数
/// </summary>
[Description("是否允许为负数:如果为Ture则可以为负数,否则只能是正数")]
public bool IsNegativeEnable
{
get { return isNegativeEnable; }
set { isNegativeEnable = value; }
}

/// <summary>
/// 构造函数,并初化值为0
/// </summary>
public NumberTextBox()
{
InitializeComponent();
this.Text = "0";
this.TextChanged += new EventHandler(NumberTextBox_TextChanged);
}

void NumberTextBox_TextChanged(object sender, EventArgs e)
{
double value;
if (this.Text == "" || this.Text=="-" || this.Text =="-0" || this.Text =="-0.")
{
return;
}
try
{
value = Convert.ToDouble(this.Text.Trim());
}
catch
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
if (value > maxValue || value < minValue)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据超出范围!", this, 1000);
this.Text = minValue.ToString();
return;
}
if (isNegativeEnable)
{

}
else
{
if (value < 0)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
}
}
/// <summary>
/// 重写OnKeyPress事件
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (isAllowKeyPress)//如果允许
{
e.Handled = true;
}
base.OnKeyPress(e);
}

protected override void OnLeave(EventArgs e)
{
if (this.Text.ToString().Trim() == "" || this.Text == "." || this.Text == "-"||this.Text=="-0" || this.Text=="-0.0" || this.Text=="-0")
{
this.Text = "0";

}
if (this.Text.ToString().IndexOf(".") == 0 && this.Text.Length > 1)
{
this.Text = "0" + this.Text;
}

if (this.Text.Trim().IndexOf("-.") != -1 && this.Text.Length > 3)
{
this.Text = this.Text.Insert(1, "0");
}
double value;
try
{
value = Convert.ToDouble(this.Text.Trim());
this.Text = value.ToString();
}
catch
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
if (value > maxValue || value < minValue)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据超出范围!", this, 1000);
this.Text = "0";
return;
}
if (isNegativeEnable)
{

}
else
{
if (value < 0)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("数据错误!", this, 1000);
this.Text = "0";
return;
}
}
base.OnLeave(e);
}

protected override void OnKeyDown(KeyEventArgs e)
{

Rectangle rct = new Rectangle(new Point(0,0), new Size(this.Width, this.Height));
Point pt = PointToClient(Cursor.Position);
if (e.KeyValue.Equals(18) || (e.KeyValue >= 112 && e.KeyValue <= 123) || e.KeyValue == 145 || e.KeyValue == 45 || e.KeyValue == 36 || e.KeyValue == 33 || e.KeyValue == 34 || e.KeyValue == 16 || e.KeyValue == 20)
{
isAllowKeyPress = false;
return;
}
if (rct.Contains(pt))
{
Point p=PointToScreen(new Point(0,0));
Cursor.Position = new Point(p.X + this.Width + 1, p.Y + this.Height + 1);
}
if (e.KeyValue.Equals(46) || e.KeyValue.Equals(8) || e.KeyValue.Equals(39) || e.KeyValue.Equals(37) || e.KeyValue.Equals(38) || e.KeyValue.Equals(40))
{
isAllowKeyPress = false;
return;
}
else if ((e.KeyValue == 189) || (e.KeyValue == 109))
{
if (IsNegativeEnable)
{
if (this.Text == "")
{
isAllowKeyPress = false;
return;
}
else
{
int i = this.Text.ToString().IndexOf("-");
if (i != -1)
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("已经存在负号了!", this, 1000);
isAllowKeyPress = true;
return;
}
else
{
isAllowKeyPress = false;
return;
}
}
}
else
{
isAllowKeyPress = true;
}

}
else if ((e.KeyValue == 46) || (e.KeyValue >= 96 && e.KeyValue <= 105) || (e.KeyValue.Equals(110)) || (e.Control == true) || (e.Control == true && (e.KeyValue.Equals(67) || e.KeyValue.Equals(86))) || (e.KeyValue == 8 || (e.KeyValue >= 48 && e.KeyValue <= 57)) || (e.KeyValue.Equals(8)) || (e.KeyValue.Equals(190)) || (e.KeyValue.Equals(13)))
{
string ss = this.Text;
int i = this.SelectionStart;
int j = this.Text.ToString().IndexOf("-");
int k = this.SelectedText.Length;
if (i == 0 && j!=-1 && k==0)
{
if (!(e.KeyValue.Equals(39) || e.KeyValue.Equals(40)))
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("不应该在负号前输入数字!.", this, 1000);
}
isAllowKeyPress = true;
}
else
{
isAllowKeyPress = false;
}
}
else
{
isAllowKeyPress = true;
this.toolTip.Show("", this, 0);
this.toolTip.Show("只能输入数字!", this, 1000);
}
if ((e.KeyValue.Equals(190) || e.KeyValue.Equals(110)))
{
int i=this.Text.ToString().IndexOf(".");
if (i == -1)
{
isAllowKeyPress = false;
return;
}
else
{
this.toolTip.Show("", this, 0);
this.toolTip.Show("已经存在小数点了!", this, 1000);
isAllowKeyPress = true;
return;
}
//i = this.SelectionStart;
//int j = this.Text.ToString().IndexOf("-");
//if (i == 0 && j != -1)
//{
// if (!(e.KeyValue.Equals(39) || e.KeyValue.Equals(40)))
// {
// this.toolTip.Show("", this, 0);
// this.toolTip.Show("不应该在负号前输入数字!.", this, 1000);
// }
// isAllowKeyPress = true;
// return;
//}
//else
//{
// isAllowKeyPress = false;
// return;
//}
}
base.OnKeyDown(e);
this.Refresh();
}

private void InitializeComponent()
{
t**.components = new System.ComponentModel.Container();
this.toolTip = new System.Windows.Forms.ToolTip(t**.components);
this.toolTip.IsBalloon = true;
this.SuspendLayout();
//
// toolTip
//
this.toolTip.AutomaticDelay = 50;
this.toolTip.AutoPopDelay = 500;
this.toolTip.BackColor = System.Drawing.Color.White;
this.toolTip.ForeColor = System.Drawing.Color.Black;
this.toolTip.InitialDelay = 100;
this.toolTip.ReshowDelay = 0;
this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Error;
this.toolTip.ToolTipTitle = "错误";
this.ResumeLayout(false);

}
}

}
瑞瑞爱吃桃
前台还是后台?

如果是前台可以直接使用验证控件+正则表达式

正则表达式:^d+$(纯数字,不含小数点)
^d+(.d+)?$(含小数点)
正则表达式写法具体看你的要求,这个你可以参考正则表达式的具体说明

如果后台也可以使用正则表达式,不过我通常比较偷懒,我会直接使用异常处理。
try
{
double x=Convert.ToDouble(xx.Text.Trim());
//直接转换,如果是数字无异常,如果不是数字会抛异常

}
catch (Exception ex)
{

}
皮皮

你可以把用户输入的字符串每次取一个出来,然后判断是不是属于0-9,如果不是的话就证明不是数字了,我给的只是原理,代码你就自己写好了

小教板

int num=-1;

if(int.TryParse(TextBox.Text,out num))

Response.Write("是数字");

else

Response.Write("不是数字");

okok云

通常我也会异常处理。

try

{

double x=(double)(textBox1.Text);

//是数字

}

catch

{

//不是数字

}

贝贝

RegularExpressionValidator

这个控件可以验证

然后写一个表达式就成了

苏州马小云

可以直接和后台属性绑定

或者用键盘输入事件

小菜G

用正则表达式,也可以呀

S笔记

RegularExpressionValidator

d+

相关推荐

indexof是什么意思

indexOf返回字符串的初始位置
2023-07-10 20:15:534

indexof是什么意思

indexof意思是什么的指数。v.某某的指标短语:1、Indexnumberofwage工资指数;翻译;工资指数英语2、indexoutofrange索引超出范围;出的指数系列3、indexarmofsextant指标杆 扩展资料 临近词: 1、indexes n.指数;标准(index的复数);索引 例句: Eachindexpartitionindexesrowsonlyfromthecorrespondingdatapartition. 每个索引分区仅从相应的`数据分区取得索引行。 2、indexed v.为……编索引;把……编入索引;指示;使……自动与价格系数挂钩;(机器)转位(index的过去式和过去分词) 例句: Itcanassociateattributeswitheachpieceofindexeddata. 它可以把属性与每条索引数据关联起来。
2023-07-10 20:16:211

indexOf()的用法,具体是什么意思??

1.indexOf(int,ch):先看第一个indexOf它返回值是int,在看它的参数(int,ch)意思就是使用者可以给参数一个‘char"字符所代表的int值,然后去从前向后找到该字符在字符串中第一次出现处的索引,当然了我们不可能记得住每一个char的值所以我们在使用时直接用String s=abcdef;  int i=s.indexOf("d")。2.indexOf(int ch,int,fromIndex):这个方法就是说从指定位置往后找返回字符在该字符串中第一次出现处的索引,比如“woaizhongguo”indexOf("o",2)那返回值就是6而不是1,也不是11。3.indexOf(Sting str):这个方法基本就类似前面的了,只不过它是在参数里给一个子字符串,然后返回该子字符串在该字符串中第一次出现处的索引,比如"woaixuexi"要查"ai"这个子字符串在整个字符串中出现的索引位置那返回值就是2。
2023-07-10 20:16:291

indexOf("x",i)是从第i开始还是从i+1开始

是从i+1开始的。因为是从指定位置后第一次出现的位置。
2023-07-10 20:16:532

indexof 和 indexofany有什么区别

区别是:indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。即indexOf()括号内所包含的字符在该字符串内的循序位置,在第几位就返回几-1,类如:str1=asdfkju,str1.indexOf("d"),则返回的值是2。如果有重复的字符出现,以第一个字符为准。如果没有找到子字符串,则返回 -1。IndexOfAny方法功能同IndexOf类似,区别在于,它可以搜索在一个字符串中,出现在一个字符数组中的任意字符第一次出现的位置。同样,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。常用的IndexOfAny重载形式有3种:(1)int IndexOfAny(char[]anyOf);(2)int IndexOfAny(char[]anyOf, int startIndex);(3)int IndexOfAny(char[]anyOf, int startIndex, int count)。在上述重载形式中,其参数含义如下:(1)anyOf:待定位的字符数组,方法将返回这个数组中任意一个字符第一次出现的位置。(2)startIndex:在原字符串中开始搜索的其实位置。(3)count:在原字符串中从起始位置开始搜索的字符数。
2023-07-10 20:17:021

JAVA中的indexOf啥意思?

返回指定字符在此字符串中第一次出现处的索引。
2023-07-10 20:17:313

javascript 中indexof 的用法

你可以考虑使用“Dojo”框架~~~,里面很多Api能够方便你使用javascript
2023-07-10 20:17:394

用indexOf还是includes

js中有两个判断数组是否存在某个元素的方法,一个是includes,一个是indexOf。 includes使用起来很方便,本身返回的也是布尔值 indexOf的话则是 两个方法似乎都能达到我们的目的,但有以下一种情况,当我们的数组里面存在 NaN 值的时候。 这个值比较特殊: 可以看出,虽然都是NaN,但却不是相等的。 然后我们再来看一下: 这里差异就出来了,明显indexOf没有对 NaN 值做处理。但也正是因为如此,indexOf的效率明显要高于includes。所以我们使用indexOf的过程中要注意 NaN 值带来的影响,避免其引起隐晦的bug。
2023-07-10 20:17:471

js中IndexOf()是干什么用的呢?怎么用?

精彩回答,留个名。。。。。。1楼很透彻,2楼很精辟。。。。。。
2023-07-10 20:18:186

请教java中indexOf()的问题

呵呵 你虽然方法中用了返回,但是你没有把值给打印出来啊,当然什么都不会显示了,用个输出语句就可以了。
2023-07-10 20:18:415

JavaScript中indexOf()的用法和详解

if(email.indexOf("@",0)==-1||email.indexOf(".",0)==-1){ alert("此为不正确的邮件格式。必须包含"@"和"."~~~~~"); return;}if(email.indexOf("@",0)==0||email.indexOf("@",0)==email.length-1){alert(""@"不能在最前端或最后端~~~~~");return;}if(email.indexOf(".",0)==0||email.indexOf(".",0)==email.length-1){alert(""."不能在最前端或最后端~~~~~");return;}if(email.indexOf(".",0)-(email.indexOf("@",0))<=0){alert(""."必须在"@"的后面~~~~~");return;}if(email.indexOf(".",0)-(email.indexOf("@",0))<=1){alert(""@"和"."中间必须有字符~~~~~");return;}就这个意思,你应该看的懂吧~~~
2023-07-10 20:18:591

为什么打开网页会出现Index of,怎么解决,以前都能打开的

给你几个函数参考下 //载入XML配置文件 var xmlObj:XML = new XML(); xmlObj.ignoreWhite = true; //判断是否从页面传来xml路径参数 if ((xmlPath == undefined) || (xmlPath == "")) { xmlObj.load("/xml/viewerDataen.xml"); } else { xmlObj.load(xmlPath); } xmlObj.onLoad = mx.utils.Delegate.create(this, init); // //判断XML加载是否成功 if (!isLoadXMLSuccess) { return false; } // // itemTotal = xmlObj.firstChild.childNodes.length; intervalTime = xmlObj.firstChild.attributes["interval"]; isRandom = Boolean(Number(xmlObj.firstChild.attributes["isRandom"])); // //把XML数据保存到数组中 for (var i = 0; i<itemTotal; i++) { xmlArray[i] = {title:"", img:"", url:"", target:"", bytesLoaded:0, bytesTotal:0, loadComplete:false, btnMC:null, showMC:null}; xmlArray[i].title = xmlObj.firstChild.childNodes[i].attributes["title"]; xmlArray[i].img = xmlObj.firstChild.childNodes[i].attributes["img"]; xmlArray[i].url = xmlObj.firstChild.childNodes[i].attributes["url"]; xmlArray[i].target = xmlObj.firstChild.childNodes[i].attributes["target"]; } //采纳哦
2023-07-10 20:19:071

java中indexof的问题

lockxxx - 经理五级 正解
2023-07-10 20:19:255

请问大神下面代码中“indexOf”是什么意思?有啥作用?

查找是否存在。如果存在返回起始位置。如果不存在,返回-1
2023-07-10 20:19:422

indexof报错

indexof报错,not a function 因为循环,把循环的值输出,没有问题,可是依然报错,后来想到会不会是类型影响的,于是把要用的indexof的值变为string类型的,报错就没有了。
2023-07-10 20:20:041

java中indexOf的使用

IndexOf 方法 返回 String 对象内第一次出现子字符串的字符位置。strObj.indexOf(subString[, startIndex])参数strObj 必选项。String 对象或文字。 subString 必选项。要在 String 对象中查找的 子字符串。 starIndex 可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。 说明indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。从左向右执行查找。否则,该方法与 lastIndexOf 相同。示例下面的示例说明了 indexOf 方法的用法。function IndexDemo(str2){var str1 = "BABEBIBOBUBABEBIBOBU"var s = str1.indexOf(str2);return(s);}
2023-07-10 20:20:173

c++中的indexof的具体用法

indexof是java体系的方法(java, javascript),C++可以用strstrchar* p = strstr(filename + strlen(filename) - 4, ".exe"); // 从倒数第四位开始查找return p == NULL;
2023-07-10 20:20:261

Java中查找字符串indexof()方法是怎么计算起始位置的

返回指定子字符串在此字符串中第一次出现处的索引。空格是字符,当然要算上.注意:索引是从0开始的比如那那个字符串"Thepiano"T的索引是0,p的索引是3"abcabcabc".indexOf("abc")=0;"abcabcabc".indexOf("abc")=-1;//找不到
2023-07-10 20:20:415

IndexOf可以用正则表达式吗

不能,因为indexOf是一个字符串方法,在JS原理中,indexOf并没有判断正则的产生的判断.如果你想用一个自已的indexOf可以这么写String.indexOf = function(reg){console.log(reg.constructor)} // 里面是可以检测 正则的
2023-07-10 20:21:001

网站首页出现index of,出不来页面,请问这是什么原因呢?php

一维坐标系指选某一坐标为坐标原点,以某个方向为正方向,选择适当的标度建立一个坐标轴,就构成了一维坐标系,适于描述物体在一维空间运动(即物体沿一条直线运动)时物体的位置二维坐标系即平面直角坐标系二维,两根的坐标轴,有平面直角坐标丶自然坐标丶极坐标等,研究平面运动时用。二维坐标系可以解释为在平面内两条相互垂直且有公共原点的数轴组成的坐标系即二维坐标系。三维笛卡儿坐标系是在二维笛卡儿坐标系的基础上根据右手定则增加第三维坐标(即Z轴)而形成的。同二维坐标系一样,AutoCAD中的三维坐标系有世界坐标系WCS(WorldCoordinateSystem)和用户坐标系UCS(UserCoordinateSystem)两种形式。
2023-07-10 20:21:122

js indexof()函数用法

<a href="http://www.163.com/">http://www.163.com/</a><a href="http://www.baidu.com/">http://www.baidu.com/</a><a href="http://www.sohu.com/">http://www.sohu.com/</a><a href="http://www.sina.com/">http://www.sina.com/</a><a href="http://localhost/">http://localhost/</a><br><a href="javascript:extractlinks()">点击测试下含有WWW的链接?</a><script language="JavaScript1.2"> <!-- function extractlinks(){ var links=document.all.tags("A") b=0 var total=links.length var win2=window.open("","","menubar,scrollbars,toolbar") win2.document.write("<font size="2">一共有"+total+"个连接</font><br>") for (i=0;i<total;i++) { if ((links[i].href).indexOf("www")!=-1) {win2.document.write("<font size="2">"+links[i]+b+"</font><br>");b++;} } } //--> </script>
2023-07-10 20:21:221

c语言indexof的问题,请指教!

#include <stdio.h> #include<string.h> main() {char str1[1001];char str2[11][1001];//记录你所有的数据组int count[11] = {0};//记录你数字的保存个数int flag[11];//出现重复的字符时不重新计算char *p = null;int index = 0;int number = 0;int x = 0;for (index = 0; index < 10; index++){ flag[index] = -1;}for (index = 0; index < 1000; index++){ p = str1[index]; while((p != null) && (p != "")) { x = *p - 48; if (flag[x] == -1) { flag[x] = 1; str2[x][count[x]] = str1[index]; count[x]++; } p++; } } p = null for (number = 0; number < 10; number++) { flag[number] = -1; }}}最后输出时,之需要将count数组中的总数输出,也可以输出str2[][1001]中所保存的每个数
2023-07-10 20:21:312

indexOf问题,帮忙解决一下。

System.out.print(a.indexOf("灵")); 试试这个,返回当前字符的位置。
2023-07-10 20:21:522

vb要实现indexof 功能该怎么做

用instr函数, 它能返回指定字符串在另一字符串的首位置如Dim a As StringDim i As Integera = "bac"i = InStr(1, a, "a")这里 i = 2.
2023-07-10 20:22:132

javascript数组有没有indexOf方法

有,Array.prototype.indexOf, 但是IE8及以下浏览器不支持
2023-07-10 20:22:201

java种String类的indexof方法

indexOf(String str, int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。这里从b开始,因此找不到a,则返回 -1。
2023-07-10 20:22:285

搜索引擎index of怎么用

index of 实际上是 阿帕奇服务器 目录文件的默认title,如果你要找MP3 ,可以搜 index of mp3实际上这种搜索早在几年前都被注意了,所以您不到多少东西。
2023-07-10 20:22:431

求java大神讲一下indexOf()的用法,结合图片讲。书上的没理解。谢谢了!

qa3w4se5dr6tf7gy8i9j[0ok]-pl[];"
2023-07-10 20:22:512

java 从一个URL中提取特定子字符串保存

我在网吧上的,所以不能做实例给你看不过可以说说自己的思路我记得session里面有一个获取当前地址的geturl的方法,你可以查查然后用一个字符串储存,在将这个String用拆分的方法取特殊的字符将其划分,就行了
2023-07-10 20:22:594

刚入门php,为什么会显示index of/

这个问题不纠结也罢。就一个简单的h标签标题。
2023-07-10 20:23:095

flash中的indexof("error")>0这句是什么意思

indexOf是在字符串查找指定的字符,如果找到则返回所在位置(正整数),否则返回-1
2023-07-10 20:23:242

我的网站有 index of,怎么取消

你在你指向的这个文件夹内防止一个空白的 index.htm页面就没有了===========================或者修改你的apache 的httpd.conf<Directory />Options FollowSymLinksAllowOverride None</Directory> 在Directory项里修改成上面的
2023-07-10 20:23:311

if (_title.indexOf("庆禧") >= 0)什么意思啊

_title.indexOf("庆禧") 返回庆禧这两个字在 _title 变量中的位置,如果存在则返回一个具体数字位置(这个位置从0开始),如果不存在则返回 -1这条语句的意思就是如果在 _title 变量中存在庆禧两个字则执行
2023-07-10 20:23:391

indexof是什么意思

  indexof意思是什么的指数。v.某某的指标短语:1、Indexnumberofwage工资指数;翻译;工资指数英语2、indexoutofrange索引超出范围;出的指数系列3、indexarmofsextant指标杆   扩展资料   临近词:   1、indexes   n.指数;标准(index的复数);索引   例句:   Eachindexpartitionindexesrowsonlyfromthecorrespondingdatapartition.   每个索引分区仅从相应的`数据分区取得索引行。   2、indexed   v.为……编索引;把……编入索引;指示;使……自动与价格系数挂钩;(机器)转位(index的过去式和过去分词)   例句:   Itcanassociateattributeswitheachpieceofindexeddata.   它可以把属性与每条索引数据关联起来。
2023-07-10 20:24:321

indexof用法

根据查询博客园网显示,indexOf有四种用法:1.indexOf(int ch) 在给定字符串中查找字符(ASCII),找到返回字符数组所对应的下标找不到返回-1;2.indexOf(String str)在给定符串中查找另一个字符串;3.indexOf(int ch,int fromIndex)从指定的下标开始查找某个字符,查找到返回下标,查找不到返回-1;4.indexOf(String str,int fromIndex)从指定的下标开始查找某个字符串。indexof是一个计算机术语,是报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
2023-07-10 20:24:391

index 和 indexOf 具体怎么使用

int indexOf(int ch,int fromIndex)函数:就是字符ch在字串fromindex位后出现的第一个位置.没有找到返加-1eg:String str="a2dfcfar1bzvb";System.out.println(str.indexOf(97,2));看这个例子,输出:6a的ASCII为97,就从d开始找a找到了输出a所在字符串的确切位置,找不到就输出-1!(java中位置第一个从0开始)String.indexOf函数用法小结1. indexOf的参数是String, startIndex: Number; indexOf的返回值为int,2. Function indexOf 包含如下几个格式:1). Strng.indexOf(substring) //搜索String中的substring,默认从0位开始;2). String.indexOf(substring, int m) //搜索String中的substring, 默认从第m位开始;public class Hehe{int i; int x; String ip= null; String input1 = null; String input2 = null; public void main(String args[]){ ip = "126.168.1.1"; i = ip.indexOf("."); x = ip.indexOf(".",i+1); input1 = ip.substring(0,i); input2 = ip.substring(i+1, x);System.out.println("the input1 is "+input1); System.out.println("the input2 is "+input2); }}结果是the input1 is 126the input2 is 168
2023-07-10 20:24:471

javascript中的indexOf函数用法

查找一个字符串在另一个字符串中首次出现的位置例子var str="123";var pos=str.indexOf("2");alert(pos)
2023-07-10 20:24:542

xlsx使用时报错indexofofundefined

你是想问xlsx使用时报错indexofofundefined怎么回事吗?xlsx使用时报错indexofofundefined是因为表格名称错了。xlsx使用时报错indexofofundefined是因为表格的名称格式错了。表格名称只能是字符串,不能是数字。xlsx是MicrosoftOfficeEXCEL2007/2010/2013/2016/2019文档的扩展名。
2023-07-10 20:25:041

登录无线路由器管理界面显示:index of ./

2023-07-10 20:25:111

我做的网站打开为什么出现Index of /为什么?求各位高手指导

我出现的问题和你的一么一样,不知道解决了没有
2023-07-10 20:25:453

indexof/ppt怎么用

been kept in readiness f
2023-07-10 20:26:061

java.lang.StringIndexOutOfBoundsException:String index out of range: -1"是什么意思?

数组越界。比如你申请了一个String s="abcd";然后你又进行了一个查找,比如int mm= s.indexOf("ddd");但是你的String 里面并没有"ddd"这个字符串,所以indexOf方法返回的是-1然后你再用这个mm做为参数访问String ,比如s.substring(mm,2);就会出现越界了。
2023-07-10 20:27:112

java获取双引号间的值

String str = "<content d="2012-04-17" o="33.900" h="35.140" c="34.810" l="32.910" v="12934" bl="" />"String [] arrStr = str.split( "\"" );arrStr [1],arrStr[3],arrStr[5]........
2023-07-10 20:28:094

java中index的用法

在JSTL中有个foreach 中有个varState="s"用<%s.index%>代表索引
2023-07-10 20:28:347

java中怎么实现在一个字符串中查找其中的关键字。

indexof方法!
2023-07-10 20:28:584

java中 如何从LIST 查找指定元素的位置

list.indexOf(你要查的元素);
2023-07-10 20:29:076

ASP.NET实现新闻页面的分页功能

//生成静态网页string path = Server.MapPath("~/news/" + folder + "/");string file_template_name = Server.MapPath("~/news/template.htm"); //新闻模版文件string file_template_content = "";StreamReader sr_reader = new StreamReader(file_template_name, Encoding.GetEncoding("gb2312"));file_template_content = sr_reader.ReadToEnd();sr_reader.Close();string[] subContent = FileSplit(content);string file_content = "";int pageNum = 0;while (pageNum10subContent[pageNum] != ""subContent[pageNum] != null)pageNum++;for (int index = 0; indexpageNum; index++){file_content = file_template_content;file_content = file_content.Replace("$$category", list_department.SelectedItem.Text); //新闻类别file_content = file_content.Replace("$$title", title); //新闻标题file_content = file_content.Replace("!--来源:$$author--", "来源:" + author); //作者file_content = file_content.Replace("$$time", time); //添加时间file_content = file_content.Replace("$$content", subContent[index]); //新闻正文string pageLink = "";int firstPage = 0;//生成页码if (index2) firstPage = index - 2;for (int i1 = firstPage; i1index + 3i1pageNum; i1++){if (i1 == index)pageLink = pageLink + "[" + (index + 1) + "]" + "nbsp; nbsp; nbsp; nbsp;";else pageLink = pageLink + "a href=" + htmlfilename + i1 + ".htm[" + (i1 + 1) + "]/anbsp; nbsp; nbsp; nbsp;";}if (indexpageNum - 1)pageLink = pageLink + "a href=" + htmlfilename + (index + 1) + ".htm" + "下一页" + "/anbsp; nbsp; nbsp; nbsp;";if (index0)pageLink = "a href=" + htmlfilename + (index - 1) + ".htm" + "上一页" + "/anbsp; nbsp; nbsp; nbsp;" + pageLink;file_content = file_content.Replace("!-- $$pageLink --", pageLink);if (index == pageNum - 1attachment_filename != nullattachment_filename != String.Empty){string[] attachment = attachment_filename.Split(new char[] { "|" });string attachmenthtml = "a href="attachment/" + attachment[0] + "" " + attachment[0] + "/a";for (int j = 1; jattachment.Length; j++)attachmenthtml = attachmenthtml + "br/ nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;a href="attachment/" + attachment[j] + "" " + attachment[j] + "/a";file_content = file_content.Replace("$$attachment", attachmenthtml);}StreamWriter sw = new StreamWriter(path + htmlfilename + index + ".htm", false, Encoding.GetEncoding("gb2312"));sw.Write(file_content);sw.Flush();sw.Close();}HyperLink1.Text = "预览: " + title;HyperLink1.NavigateUrl = "/news/" + folder + "/" + htmlfilename + "0" + ".htm";HyperLink1.Visible = true;txt_time.Text = DateTime.Now.ToString("yyyy-MM-dd");txt_title.Text = "";txt_author.Text = "";FreeTextBox1.Text = "";}catch (Exception e){}}//将正文分成多个页面protected string[] FileSplit(string fileContent){int fileIndex = 0;string[] splitedFile = new string[10];while (fileContent.Length1500fileIndex9) //每页至少1500个字符{if (fileContent.IndexOf("P", 1500)0) break;splitedFile[fileIndex] = fileContent.Substring(0, fileContent.IndexOf("P", 1500));fileContent = fileContent.Remove(0, splitedFile[fileIndex].Length);fileIndex++;}splitedFile[fileIndex] = fileContent; //超过9页,剩下部分全放第十页return splitedFile;}}
2023-07-10 20:29:341

String index out of range: -1

索引超出范围啦,不能是-1
2023-07-10 20:29:434

在java中怎么判断一个字符是否在另一个字符的后面

char a = "a";char b = "b";return a>b;
2023-07-10 20:29:522

怎样通过下标获取string中的单个unicode字符

//不是特别明白你循环输出的意思,如果只是要找到下标的话,用String类的indexOf方法就可以了//按你的意思写了一下,代码中包括对indexOf方法的使用,你看一下吧。public class StringFun {public static void main(String[] args) {String str="每次和小朋友玩捉迷藏的时候,我总是等他们先藏好,我就回家。";System.out.println(str.indexOf("和"));System.out.println(str.indexOf("小朋友"));stringFun("次",str);}/*** 求出c字符在str串中的下标并以循环的方式输出str串*/private static void stringFun(char c, String str) {boolean flag=true;int index=-1; //用于保存初次比中的下标值char[] chars=str.toCharArray();for(int i=0;i<chars.length;i++) {if(chars[i]==c&&flag) {index=i;flag=false;}System.out.print(chars[i]);}if(index!=-1) System.out.println(" ""+c+""字符在串中初次出现的下标为:"+index);else System.out.println(" ""+c+""字符未在串中出现过");}}
2023-07-10 20:29:591