如何在C#中获取个人字符? [英] How To Get Individial Characters in C#?

查看:62
本文介绍了如何在C#中获取个人字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello World!

我已经编写了通过蓝牙从LEGO MINDSTORMS NXT设备读取和写入数据的代码.接收到的数据以ascii格式包含,它们包含多个字节.每个字节对此都有含义..

长度是设备名称,我需要将其转换为字符串,但是我需要获取包含ASCII值的文本框值的这一部分.

你有什么办法做到这一点...我尝试过子字符串,但它不起作用?

帮助!!有什么想法吗?

Travis

Hello World!

I have written code to read and write data from a LEGO MINDSTORMS NXT Device via bluetooth. The data recieved is in ascii and they contain multiple bytes..each byte has a meaning to it..

One lenght is the device name and i need to convert it a string but i need to get this part of the textbox value which contains the ASCII value.

Do you have any way to do this...i have tried substring but it does not work?

HELP!! Any ideas?

Travis

推荐答案

嘿!通过在字符串实例标识符上使用方括号,可以更好地访问字符串中的单个字符.就像
hey! its bettter to access Individual character from string by using Square bracket on the string instance identifier. like
string str = "ABSDEFG";
char firstvalue = str[0];
char secondvalue = str[1];
char Thirdvalue = str[2];


对于最后一个值,我们可以使用 str[str.length -1];


and for last value is we can use str[str.length -1];


如果数据以字节为单位接收,您要做的就是将其转换为字符串:
If the data is received as bytes, all you need to do is convert it to a string:
byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);




我有...,但是如何获得字符串的各个部分"




"I have....but how do i get individual parts of the string"

byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);
string t = s.Substring(0, 5);       // First five characters
string u = s.Substring(2, 3);       // Third, fourth and fifth characters
string v = s.Substring(10);         // All characters from the 11th to the end
char x = s[4];                      // Fifth character


等等...


And so on...


这篇关于如何在C#中获取个人字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆