如何删除给定字符串中的最后一个逗号?

时间:2016-11-14 15:35:20

标签: excel-vba vba excel

我尝试使用以下代码,但无法删除逗号。请帮忙。

Sub removelastcommas()

    Dim i As Integer, str As String

    str = Range("A1")

    For i = Len(str) To 1
        If Mid(str, i, 1) <> "," Then
            Exit For
        End If
    Next
    Range("b1") = Left(str, i)

End Sub

2 个答案:

答案 0 :(得分:2)

这将删除字符串中的最后一个逗号:

Sub removelastcommas()
    Dim i As Integer, str As String
    str = Range("A1")
        For i = Len(str) To 1 Step -1
            If Mid(str, i, 1) = "," Then
                Range("B1").Value = Left(str, i - 1) & Mid(str, i + 1)
                Exit Sub
            End If
        Next i
End Sub

enter image description here

答案 1 :(得分:2)

使用public class FooUpdatesHandler extends ConcurrentTextWebSocketHandler { // ConcurrentTextWebSocketHandler taken from https://github.com/RWTH-i5-IDSG/BikeMan (Apache License version 2.0) private static final Logger logger = LoggerFactory.getLogger(FooUpdatesHandler.class); private static final ConcurrentHashMap<String, ConcurrentHashMap<String, WebSocketSession>> fooSubscriptions = new ConcurrentHashMap<>(); public static void sendFooToSubscribers(Foo foo) { Map<String, WebSocketSession> sessionMap = fooSubscriptions.get(foo.getId()); if (sessionMap != null) { String fooJson = null; try { fooJson = new ObjectMapper().writeValueAsString(foo); } catch (JsonProcessingException ignored) { return; } for (WebSocketSession subscription : sessionMap.values()) { try { logger.info("[fooId={} sessionId={}] Sending foo...", foo.getId(), subscription.getId()); subscription.sendMessage(new TextMessage(fooJson)); logger.info("[fooId={} sessionId={}] Foo send.", foo.getId(), subscription.getId()); } catch (IOException e) { logger.error("Socket sendFooToSubscribers [fooId={}], exception: ", foo.getId(), e); } } } } } 函数(不使用循环)的另一个选项是:

InStrRev