从字符串中获取最后一个数字并将其增加一个

时间:2016-04-28 10:35:59

标签: vb.net

我的功能有些问题。在数据库中,我可能有不同的数字。例如下面:(我知道它看起来很奇怪)

12 312323.3
013.43.9
3.23.14353.55 WHATEVER 345.193
728937.3
87.3 ojojo 23.434blabla 24.424.7

我需要做的是在LAST DOT之后增加数量,所以只需加上+1。

问题是它在点数超过一位之后才起作用。

这是我目前的代码:

Dim inputValue as String = "34.234234.6.12"
'--Get Last char from string and add 1 to it
Dim lastChar As String = CInt(CStr(inputValue.Last)) + 1
'--Remove last char and add lastChar
Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & lastChar  
            Return nextCombinNummer

我认为问题是lastValue.Last + 1,因为它只需要一个数字,而且当我通过子字符串删除最后一个数字但只有2个将被删除。

你可以帮我解决这个问题吗?如何始终从字符串的最后一个点后取数字,然后将该数字增加1并返回新的整数?

修改

我想我能够获得并增加数量,但仍然不知道如何删除并将其放在最后:

认为没问题:

Dim inputValue as String = "34.234234.6.12"
Dim number As String = inputValue .Substring(inputValue .LastIndexOf("."c) + 1)                                                                
Dim numberIncreased as integer = CInt(number) + 1

'如何正确地做到这一点? :

Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & numberIncreased  

2 个答案:

答案 0 :(得分:0)

我使用以下内容使用Int32.TryParseString.JoinDim numbers As New List(Of String) From {"12.312323.3", "013.43.9", "3.231435355345.193", "728937.3", "87.323.43424.424.7"} for i As Int32 = 0 To numbers.Count -1 Dim num = numbers(i) Dim token = num.Split("."c) dim lastNum = token.Last() ' or token(token.Length-1) Dim n As Int32 If int32.TryParse(lastNum, n) n += 1 token(token.Length-1) = n.ToString() End If numbers(i) = string.Join(".", token) Next

function drawFreeHand()
{

    //the polygon
    poly=new google.maps.Polyline({map:map,clickable:false});
    
    //move-listener
    var move=google.maps.event.addListener(map,'mousemove',function(e){
        poly.getPath().push(e.latLng);
    });
    
    //mouseup-listener
    google.maps.event.addListenerOnce(map,'mouseup',function(e){
        google.maps.event.removeListener(move);
        var path=poly.getPath();
        poly.setMap(null);
        poly=new google.maps.Polygon({map:map,path:path});
        
        
        google.maps.event.clearListeners(map.getDiv(), 'mousedown');
        
        enable()
    });
}

function disable(){
  map.setOptions({
    draggable: false, 
    zoomControl: false, 
    scrollwheel: false, 
    disableDoubleClickZoom: false
  });
}

function enable(){
  map.setOptions({
    draggable: true, 
    zoomControl: true, 
    scrollwheel: true, 
    disableDoubleClickZoom: true
  });
}


function initialize() 
{
  var mapOptions = {
    zoom: 14,
    center: new google.maps.LatLng(52.5498783, 13.425209099999961),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
       
  //draw
  $("#drawpoly a").click(function(e) {
    e.preventDefault();

    disable()

    google.maps.event.addDomListener(map.getDiv(),'mousedown',function(e){
      drawFreeHand()
    });

  });

}

google.maps.event.addDomListener(window, 'load', initialize);

答案 1 :(得分:0)

一个简单的解决方案是将Integer转换为字符串的最后一部分,添加一个,然后重新构造字符串:

'Original Value
Dim val As String = "123.456.789"
'We take only the last part and add one
Dim nb = Integer.Parse(val.Substring(val.LastIndexOf(".") + 1)) + 1
'We recompose the string
Dim FinalVal As String = val.Substring(0, val.LastIndexOf(".") + 1) & nb.ToString()