xiaxun 发表于 2010-8-29 16:35

【求助】输入abcs557898,怎么截取557898?


vs2008 c#.

有一个文本框textbox1用来接收数字!
[color=#ff6600]textbox1的数字长度不一定![/color]
[color=#ee3d11]textbox1接收的内容和长度都不一定的!
用substring只能指定地方呀!![/color]


现在要实现:
bb=textbox1houm后面的6个数字,

比如输入:123456789,那么bb=6789
比如输入:128970,那么bb=128970
比如输入:9898001,那么bb=898001
输入的数字都是不小于6位数的!

怎么用呀,
有函数可以做到吗!
还是用什么循环呀!
谢谢!


makebest 发表于 2010-8-29 21:52

给出的三个例子都没有规律, 晕了

烟雨袅袅 发表于 2010-8-30 03:30

[quote]bb=textbox1houm后面的6个数字,

比如输入:123456789,那么bb=6789
比如输入:128970,那么bb=128970
比如输入:9898001,那么bb=898001
输入的数字都是不小于6位数的![/quote]第一个例子是不是错了
取最后6位 textbox1.text.Substring(textbox1.text.Length - 6);

xiaxun 发表于 2010-8-30 08:32

[quote][b]以下是引用[u]makebest[/u]在2010-8-29 21:52:24的发言:[/b]

给出的三个例子都没有规律, 晕了[/quote]你说的什么意思呢呀

xiaxun 发表于 2010-8-30 08:33

本来textebox1的内容是用户随便输入的
哪里有规律呀

野比 发表于 2010-8-30 23:56

最近用正则用的很爽,顺便帮你匹配了下。。
匹配用例
[quote]123456789
128970
9898001
899[/quote]

匹配模式
[quote]\d{6}\b[/quote]

匹配参数为 [b]Multiline[/b] 多行模式(这个无所谓,单行应用直接忽略)

匹配结果(3个匹配):
[quote]456789
128970
898001[/quote]

可见,完全正确,最后一个899因为位数不足,所以没有识别。

DONE。。。

野比 发表于 2010-8-31 00:01

说真的,这点工作真还没必要用到正则匹配。。。牛刀小试。。

xiaxun 发表于 2010-8-31 09:11

什么意思呀,不明白

Fieldens 发表于 2010-8-31 16:12

用char.IsDigital取

Fieldens 发表于 2010-8-31 16:14

string str = Console.ReadLine();
            string str2="";
            char[] ach = str.ToCharArray();
            for (int i = 0; i < ach.Length; i++)
            {
                if (char.IsNumber(ach[i]))
                {
                    str2 += ach[i].ToString();
                }
            }
            Console.WriteLine(str2);

xiaxun 发表于 2010-8-31 16:17

[quote][b]以下是引用[u]Fieldens[/u]在2010-8-31 16:14:16的发言:[/b]

string str = Console.ReadLine();
            string str2="";
            char[] ach = str.ToCharArray();
            for (int i = 0; i < ach.Length; i++)
            {
                if (char.IsNumber(ach))
                {
                    str2 += ach.ToString();
                }
            }
            Console.WriteLine(str2);[/quote]

那如果我输入的全部是 字母或者全部是数字怎么截取!!

str2=qwertyuiiop
str2=112233445566778

我就想截取后面6个字符!!

Fieldens 发表于 2010-8-31 16:18

自己在最后写个截取不就行了

xiaxun 发表于 2010-8-31 16:21

[quote][b]以下是引用[u]Fieldens[/u]在2010-8-31 16:18:41的发言:[/b]

自己在最后写个截取不就行了[/quote]怎么在最后写个截取呀

谢谢!!!

Fieldens 发表于 2010-8-31 16:22

[em13]
i 已经 服 了 u

xiaxun 发表于 2010-8-31 16:24

[quote][b]以下是引用[u]Fieldens[/u]在2010-8-31 16:22:55的发言:[/b]

[em13]
i 已经 服 了 u[/quote]不要这样吗

你有qq吗 我直接问你好了

你太好了

谢谢!

yihengyy 发表于 2010-8-31 21:23

string a = "";
            int b=0;
            if(textBox2.Text.Trim()!="")
            b=(int)textBox2.Text;
            a = textBox1.Text.Trim();
            if (a != "")
            {
                if (a.Length > b)
                {

                    MessageBox.Show(+a.Substring(a - b - 1, a - b));
                }
            }

yihengyy 发表于 2010-8-31 21:27

回复 楼主 xiaxun

string a = "";
            int b=0;
            if(textBox2.Text.Trim()!="")
            b=(int)textBox2.Text;
            a = textBox1.Text.Trim();
            if (a != "")
            {
                if (a.Length > b)
                {

                    MessageBox.Show(a.Substring(a - b - 1, a - b));
                }
            }

yihengyy 发表于 2010-8-31 21:42

string a = "";
            int b=0;
            if(textBox2.Text.Trim()!="")
            b=Convert.ToInt16(textBox2.Text);
            a = textBox1.Text.Trim();
            if (a != "")
            {
                if (a.Length > b)
                {
                    MessageBox.Show(a.Substring(a.Length - b,  b));
                }
            }

xiaxun 发表于 2010-8-31 22:39

[quote][b]以下是引用[u]yihengyy[/u]在2010-8-31 21:42:31的发言:[/b]

string a = "";
            int b=0;
            if(textBox2.Text.Trim()!="")
            b=Convert.ToInt16(textBox2.Text);
            a = textBox1.Text.Trim();
            if (a != "")
            {
                if (a.Length > b)
                {
                    MessageBox.Show(a.Substring(a.Length - b,  b));
                }
            }[/quote]你的代码是什么意思呀

yihengyy 发表于 2010-9-1 08:39

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string a = "";
            int b = 0;
            if (textBox2.Text.Trim() != "")
                b = Convert.ToInt16(textBox2.Text);
            a = textBox1.Text.Trim();
            if (a != "")
            {
                if (a.Length > b)
                {
                    MessageBox.Show(a.Substring(a.Length - b, b));
                }
            }
        }
    }
}
这是完整的代码,再不明白我就没办法让你明白了

页: [1] 2

编程论坛