阵列端部偏移量 在C++中,我可以使用指针算法从数组开始位置到结束位置。你在C#做什么来完成同样的事情 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static string GetText(byte[] value, uint startPos) { // TODO - What goes here? return ""; } static void Main(string[] args) { byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 }; string result = GetText(value, 1); Console.WriteLine(result); } } }

阵列端部偏移量 在C++中,我可以使用指针算法从数组开始位置到结束位置。你在C#做什么来完成同样的事情 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static string GetText(byte[] value, uint startPos) { // TODO - What goes here? return ""; } static void Main(string[] args) { byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 }; string result = GetText(value, 1); Console.WriteLine(result); } } },c#,C#,预期输出: BBB (检查startPos是否在0和长度-1之间) 或与 (检查startPos是否在0和长度-1之间) 或与 我相信你能用你的努力实现你所需要的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program {

预期输出: BBB

(检查startPos是否在0和长度-1之间)

或与

(检查startPos是否在0和长度-1之间)

或与


我相信你能用你的努力实现你所需要的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static string GetText(byte[] value, int startPos)
        {
            if(startPos >= 0 && startPos <= value.Length)
            {
               return System.Text.Encoding.UTF8.GetString(value).Substring(startPos);
            }
            else
            {
               return string.Empty;
            }
        }

        static void Main(string[] args)
        {
            byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 };
            string result = GetText(value, 1);
            Console.WriteLine(result);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EAPP1
{
班级计划
{
静态字符串GetText(字节[]值,int startPos)
{

如果(startPos>=0&&startPos,我相信您可以用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static string GetText(byte[] value, int startPos)
        {
            if(startPos >= 0 && startPos <= value.Length)
            {
               return System.Text.Encoding.UTF8.GetString(value).Substring(startPos);
            }
            else
            {
               return string.Empty;
            }
        }

        static void Main(string[] args)
        {
            byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 };
            string result = GetText(value, 1);
            Console.WriteLine(result);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EAPP1
{
班级计划
{
静态字符串GetText(字节[]值,int startPos)
{

如果(StestPoS>=0 & & StpPOS)你将永远不会在C++中得到“BBB”。你会得到“ABBB”。.
.Length
-1将为您提供数组中的最后一个索引值。因此,如果您试图将字节转换为字符并将其附加到字符串,则可以使用
for循环,从uint startPos开始循环,直到
value.Length-1
@jdweng他希望跳过字符,直到索引为startPos,在本例中为1,跳过使用ADo时,您希望1是指字节偏移量还是字符偏移量?它们可能非常不同。一般来说,您可能希望熟悉LINQ,它的方法有
Skip
ToArray()
ToList()
。在这种情况下:
value.Skip(startPos)将返回一个枚举字节,从代码> > StestPoS<代码>到最后,然后使用标准的字节到文本转换。在C++中,你永远不会得到“BBB”。你会得到“ABBB”。.
.Length
-1将为您提供数组中的最后一个索引值。因此,如果您试图将字节转换为字符并将其附加到字符串,则可以使用
for循环,从uint startPos开始循环,直到
value.Length-1
@jdweng他希望跳过字符,直到索引为startPos,在本例中为1,跳过使用ADo时,您希望1是指字节偏移量还是字符偏移量?它们可能非常不同。一般来说,您可能希望熟悉LINQ,它的方法有
Skip
ToArray()
ToList()
。在这种情况下:
value.Skip(startPos)
将从
startPos
返回一个可枚举的字节,然后使用标准的字节到文本转换。您还可以使用
GetString
覆盖,该覆盖接受
index
count
参数point make,edited.startPos-value.Length向后看,但我明白了要点。您还可以使用e
GetString
重写,它接受
index
count
参数point make,edited.startPos-value.Length向后看,但我得到了要点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static string GetText(byte[] value, int startPos)
        {
            if(startPos >= 0 && startPos <= value.Length)
            {
               return System.Text.Encoding.UTF8.GetString(value).Substring(startPos);
            }
            else
            {
               return string.Empty;
            }
        }

        static void Main(string[] args)
        {
            byte[] value = new byte[] { 0x41, 0x42, 0x42, 0x42 };
            string result = GetText(value, 1);
            Console.WriteLine(result);
        }
    }
}