用C#替换字符串的一部分?

时间:2018-01-07 07:02:37

标签: c# .net

在我的游戏中,我有衣服玩家佩戴的图形代码,所以就像......

hd-185-1.ch-235-1408.lg-3116-85-1408.ha-1002-1408.sh-3115-1408-1408.ca-1805-64.
hd-185-12.ch-235-1408.lg-3116-85-1408.ha-1002-1408.sh-3115-1408-1408.ca-1805-64.
hr-165-52.hd-205-30.ca-1802-63.wa-2012-1408.lg-270-63.ha-3655-0.ch-806-1412.sh-305-110.he-1601-0

高清用于头部,ha用于头发,sh用于鞋子,lg用于腿部等等。我试图通过更换部分字符串来替换玩家外观的肤色...

这是我尝试过的......

public string GetHead()
{
    return FigureCode.Split('.').FirstOrDefault(x => x.StartsWith("hd"));
}

public void UpdateSkin(string skinCode)
{
    var headPart = GetHead();

    if (string.IsNullOrEmpty(headPart))
    {
        return;
    }

    var skin = headPart.Split('-')[2];

    FigureCode = FigureCode.Replace(headPart, headPart.Replace(skin, skinCode));
}

这适用于这个问题中的第一个图形代码示例(IE hd-185-1.ch-235-1408.lg-3116-85-1408.ha-1002-1408.sh-3115-1408-1408.ca-1805-64.),但其余部分是混乱的,有人能理解为什么吗?我做错了什么,更换了重复的ID?

图形代码可以是每个用户通常具有完全不同的任何东西。

1 个答案:

答案 0 :(得分:0)

我认为正则表达式解决方案适用于此:

(hd-\d+-)\d+

这基本上是找到一个匹配hd-\d+-的子字符串(hd后跟" - "后跟一个数字后跟" - "后跟另一个数字),并将其替换为所有组1(using System.Text.RegularExpressions)加上您的皮肤代码。

请记住private stream: MediaStream; private recordRTC: any; @ViewChild('video') video; ngAfterViewInit() { // set the initial state of the video let video:HTMLAudioElement = this.video.nativeElement; video.muted = false; video.controls = true; video.autoplay = false; } toggleControls() { let video: HTMLAudioElement = this.video.nativeElement; video.muted = !video.muted; video.controls = !video.controls; video.autoplay = !video.autoplay; } successCallback(stream: MediaStream) { var options = { mimeType: 'audio/webm', // or video/webm\;codecs=h264 or video/webm\;codecs=vp9 bitsPerSecond: 128000 // if this line is provided, skip above two }; this.stream = stream; this.recordRTC = RecordRTC(stream, options); this.recordRTC.startRecording(); let video: HTMLAudioElement = this.video.nativeElement; video.src = window.URL.createObjectURL(stream); this.toggleControls(); } errorCallback() { //handle error here } processVideo(audioVideoWebMURL) { let video: HTMLAudioElement = this.video.nativeElement; let recordRTC = this.recordRTC; video.src = audioVideoWebMURL; this.toggleControls(); var recordedBlob = recordRTC.getBlob(); recordRTC.getDataURL(function (dataURL) { }); } startRecording() { let mediaConstraints = { audio: true }; navigator.mediaDevices .getUserMedia(mediaConstraints) .then(this.successCallback.bind(this), this.errorCallback.bind(this)); } stopRecording() { let recordRTC = this.recordRTC; recordRTC.stopRecording(this.processVideo.bind(this)); let stream = this.stream; stream.getAudioTracks().forEach(track => track.stop()); }