ฉันจะแยกsubstringตัวอักษรที่ประกอบด้วยตัวอักษรหกตัวขวาสุดออกจากตัวอักษรอื่นได้stringอย่างไร
Ex: "PER 343573"สตริงของฉันคือ "343573"ตอนนี้ผมต้องการที่จะดึงเท่านั้น
ฉันจะทำเช่นนี้ได้อย่างไร?
string endOfString = Strings.Right(wholeString, 6);
                ฉันจะแยกsubstringตัวอักษรที่ประกอบด้วยตัวอักษรหกตัวขวาสุดออกจากตัวอักษรอื่นได้stringอย่างไร
Ex: "PER 343573"สตริงของฉันคือ "343573"ตอนนี้ผมต้องการที่จะดึงเท่านั้น
ฉันจะทำเช่นนี้ได้อย่างไร?
string endOfString = Strings.Right(wholeString, 6);
                คำตอบ:
string SubString = MyString.Substring(MyString.Length-6);เขียนวิธีการขยายเพื่อแสดงRight(n);ฟังก์ชัน ฟังก์ชันควรจัดการกับสตริงว่างหรือว่างที่ส่งคืนสตริงว่างสตริงที่สั้นกว่าความยาวสูงสุดที่ส่งคืนสตริงเดิมและสตริงที่ยาวกว่าความยาวสูงสุดที่ส่งคืนความยาวสูงสุดของอักขระขวาสุด
public static string Right(this string sValue, int iMaxLength)
{
  //Check if the value is valid
  if (string.IsNullOrEmpty(sValue))
  {
    //Set valid empty string as string could be null
    sValue = string.Empty;
  }
  else if (sValue.Length > iMaxLength)
  {
    //Make the string no longer than the max length
    sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
  }
  //Return the string
  return sValue;
}sValue.Length > iMaxLengthก่อนที่จะมีการเรียกสตริงย่อย!
                    น่าจะดีกว่าที่จะใช้วิธีการขยาย:
public static class StringExtensions
{
    public static string Right(this string str, int length)
    {
        return str.Substring(str.Length - length, length);
    }
}การใช้งาน
string myStr = "PER 343573";
string subStr = myStr.Right(6);str.Length - lengthอาจเป็นลบ พารามิเตอร์ที่สองเป็นทางเลือก ไม่ต้องผ่านความยาว
                    using System;
public static class DataTypeExtensions
{
    #region Methods
    public static string Left(this string str, int length)
    {
        str = (str ?? string.Empty);
        return str.Substring(0, Math.Min(length, str.Length));
    }
    public static string Right(this string str, int length)
    {
        str = (str ?? string.Empty);
        return (str.Length >= length)
            ? str.Substring(str.Length - length, length)
            : str;
    }
    #endregion
}ไม่ควรเกิดข้อผิดพลาดให้คืนค่า null เป็นสตริงว่างส่งกลับค่าที่ถูกตัดหรือค่าฐาน ใช้มันเช่น "testx" .Left (4) หรือ str.Right (12);
String mystr = "PER 343573";
String number = mystr.Substring(mystr.Length-6);แก้ไข: ช้าเกินไป ...
หากคุณไม่แน่ใจในความยาวของสตริงของคุณ แต่คุณแน่ใจว่ามีการนับจำนวนคำ (มักจะมี 2 คำในกรณีนี้เช่น "xxx yyyyyy") คุณควรใช้การแยก
string Result = "PER 343573".Split(" ")[1];สิ่งนี้จะส่งกลับคำที่สองของสตริงของคุณเสมอ
นี่ไม่ใช่สิ่งที่คุณต้องการ แต่เพียงแค่ดูตัวอย่างดูเหมือนว่าคุณกำลังมองหาส่วนตัวเลขของสตริง
หากเป็นเช่นนี้เสมอวิธีที่ดีก็คือการใช้นิพจน์ทั่วไป
var regex= new Regex("\n+");
string numberString = regex.Match(page).Value;ใช้สิ่งนี้:
String text = "PER 343573";
String numbers = text;
if (text.Length > 6)
{
    numbers = text.Substring(text.Length - 6);
}คาดเดาตามความต้องการของคุณ แต่นิพจน์ทั่วไปต่อไปนี้จะให้เฉพาะกับ 6 ตัวอักษรและตัวเลขก่อนที่จะสิ้นสุดของสตริงและจะไม่มีการจับคู่เป็นอย่างอื่น
string result = Regex.Match("PER 343573", @"[a-zA-Z\d]{6}$").Value;เนื่องจากคุณใช้. NET ซึ่งคอมไพล์ไปยังMSILเพียงอ้างอิงMicrosoft.VisualBasicและใช้Strings.Rightวิธีการในตัวของ Microsoft :
using Microsoft.VisualBasic;
...
string input = "PER 343573";
string output = Strings.Right(input, 6);ไม่จำเป็นต้องสร้างวิธีการขยายแบบกำหนดเองหรืองานอื่น ๆ ผลลัพธ์ที่ได้คือการอ้างอิงหนึ่งบรรทัดและโค้ดง่ายๆหนึ่งบรรทัด
ในฐานะที่เป็นข้อมูลเพิ่มเติมเกี่ยวกับเรื่องนี้โดยใช้วิธีการ Visual Basic ด้วย C # ได้รับการบันทึกไว้ในที่อื่น โดยส่วนตัวฉันสะดุดกับมันก่อนเมื่อพยายามแยกวิเคราะห์ไฟล์และพบว่าเธรด SO นี้เกี่ยวกับการใช้Microsoft.VisualBasic.FileIO.TextFieldParserคลาสเพื่อเป็นประโยชน์อย่างยิ่งสำหรับการแยกวิเคราะห์ไฟล์. csv
var str = "PER 343573";
var right6 = string.IsNullOrWhiteSpace(str) ? string.Empty 
             : str.Length < 6 ? str 
             : str.Substring(str.Length - 6); // "343573"
// alternative
var alt_right6 = new string(str.Reverse().Take(6).Reverse().ToArray()); // "343573"สิ่งนี้รองรับอักขระจำนวนเท่าใดก็ได้ในไฟล์str. รหัสทางเลือกไม่รองรับnullสตริง และครั้งแรกเร็วกว่าและวินาทีมีขนาดกะทัดรัดกว่า
ฉันชอบอันที่สองถ้ารู้strสตริงสั้น ๆ ถ้าเป็นสตริงยาวอันแรกจะเหมาะสมกว่า
เช่น
var str = "";
var right6 = string.IsNullOrWhiteSpace(str) ? string.Empty 
             : str.Length < 6 ? str 
             : str.Substring(str.Length - 6); // ""
// alternative
var alt_right6 = new string(str.Reverse().Take(6).Reverse().ToArray()); // ""หรือ
var str = "123";
var right6 = string.IsNullOrWhiteSpace(str) ? string.Empty 
             : str.Length < 6 ? str 
             : str.Substring(str.Length - 6); // "123"
// alternative
var alt_right6 = new string(str.Reverse().Take(6).Reverse().ToArray()); // "123"ใช้สิ่งนี้:
string mystr = "PER 343573";
int number = Convert.ToInt32(mystr.Replace("PER ",""));
อีกหนึ่งทางออกที่ไม่อาจกล่าวถึง
S.Substring(S.Length < 6 ? 0 : S.Length - 6)วิธีที่ปลอดภัยเป็นโมฆะ:
สตริงที่สั้นกว่าความยาวสูงสุดที่ส่งคืนสตริงเดิม
วิธีการขยายสายอักขระขวา
public static string Right(this string input, int count) =>
    String.Join("", (input + "").ToCharArray().Reverse().Take(count).Reverse());วิธีการขยายสตริงด้านซ้าย
public static string Left(this string input, int count) =>
    String.Join("", (input + "").ToCharArray().Take(count));นี่คือวิธีแก้ปัญหาที่ฉันใช้ ... ตรวจสอบว่าความยาวของสตริงอินพุตไม่ต่ำกว่าความยาวที่ถาม วิธีแก้ปัญหาที่ฉันเห็นโพสต์ข้างต้นไม่ได้คำนึงถึงสิ่งนี้อย่างน่าเสียดายซึ่งอาจนำไปสู่ข้อขัดข้อง
    /// <summary>
    /// Gets the last x-<paramref name="amount"/> of characters from the given string.
    /// If the given string's length is smaller than the requested <see cref="amount"/> the full string is returned.
    /// If the given <paramref name="amount"/> is negative, an empty string will be returned.
    /// </summary>
    /// <param name="string">The string from which to extract the last x-<paramref name="amount"/> of characters.</param>
    /// <param name="amount">The amount of characters to return.</param>
    /// <returns>The last x-<paramref name="amount"/> of characters from the given string.</returns>
    public static string GetLast(this string @string, int amount)
    {
        if (@string == null) {
            return @string;
        }
        if (amount < 0) {
            return String.Empty;
        }
        if (amount >= @string.Length) {
            return @string;
        } else {
            return @string.Substring(@string.Length - amount);
        }
    }นี่คือวิธีที่ฉันใช้: ฉันชอบทำให้สิ่งต่างๆเรียบง่าย
private string TakeLast(string input, int num)
{
    if (num > input.Length)
    {
        num = input.Length;
    }
    return input.Substring(input.Length - num);
}แค่คิด:
public static string Right(this string @this, int length) {
    return @this.Substring(Math.Max(@this.Length - length, 0));
}//s - your string
//n - maximum number of right characters to take at the end of string
(new Regex("^.*?(.{1,n})$")).Replace(s,"$1")โดยไม่ต้องใช้ตัวแปลงบิตและการเปลี่ยนบิต (ต้องแน่ใจว่าเข้ารหัส) นี่เป็นวิธีที่เร็วที่สุดที่ฉันใช้เป็นวิธีการขยาย 'ขวา'
string myString = "123456789123456789";
if (myString > 6)
{
        char[] cString = myString.ToCharArray();
        Array.Reverse(myString);
        Array.Resize(ref myString, 6);
        Array.Reverse(myString);
        string val = new string(myString);
}Array.Reverseรับอาร์เรย์ไม่ใช่สตริงและif (myString.length > 6). นอกจากข้อผิดพลาดทางไวยากรณ์เหตุใดจึงเป็นวิธีที่เร็วที่สุด แน่นอนว่าการใช้สตริงย่อยจะเป็นวิธีที่ดีกว่าโดยไม่จำเป็นต้องมีการคัดลอกอาร์เรย์ทั้งหมดนี้
                    ฉันใช้ Min เพื่อป้องกันสถานการณ์เชิงลบและจัดการสตริงว่าง
// <summary>
    /// Returns a string containing a specified number of characters from the right side of a string.
    /// </summary>
    public static string Right(this string value, int length)
    {
        string result = value;
        if (value != null)
            result = value.Substring(0, Math.Min(value.Length, length));
        return result;
    }using Microsoft.visualBasic;
 public class test{
  public void main(){
   string randomString = "Random Word";
   print (Strings.right(randomString,4));
  }
 }ผลลัพธ์คือ "Word"